CS10 Communication
CS10 Communication
Akshaya Ganesan
Assistant Professor[Off-Campus]
BITS-Pilani
Agenda
• Service invocations
• Communication between services
• Synchronous and Asynchronous communication
• HTTP Request Response
• HTTP Polling
• Messaging based communication
Service invocations
How do you call a service from a client
Axios is a Javascript library used to make HTTP requests
Fetch API is built into most modern browsers to make HTTP requests
Communication in a service based
• Services must interact using an inter-process communication protocol such as HTTP, AMQP, or
a binary protocol, depending on the nature of each service.
Interaction Styles
• Synchronous—The client expects a timely response from the service and might even block
while it waits.
• Asynchronous—The client doesn’t block, and the response, if any, isn’t necessarily sent
immediately.
Request/response communication
with HTTP and REST
Long running Transactions
API that performs tasks that could run longer than the request timeout limit.
• Optimize requests and responses that involve large objects
• Implement partial responses for clients that don't support asynchronous operations
• Support pagination for requests that may return large numbers of objects
• An HTTP 202 response should indicate the location and frequency that the client should poll for
the response.
• It should have the following additional headers:
• Location: A URL the client should poll for a response status.
• Retry-After: This header is designed to prevent polling clients from overwhelming the back-end with
retries.
Webhook
The webhook pattern for long-running tasks and asynchronous processing.
• A webhook is an HTTP-based callback function that allows lightweight, event-driven
communication between 2 application programming interfaces (APIs).
• This callback is an HTTP POST that sends a message to a URL when an event happens.
Webhooks Example
Source: The Design of Web APIs by Arnaud Lauret
More on Webhooks
• “client-side” application is the one making the request to the API on the “server-side”.
• The “client-side” must be running a server, and the “server-side” must be running a server.
• The “client-side” application makes an API request to the “server-side” server, and sends the “server-
side” server a “webhook” to call once the “server-side” wants to notify the “client-side” application of
some “event”.
• Once the “event” occurs, and the “server-side” application calls the “webhook” url, the server that is
running on the “client-side” application will “receive” that “webhook” notification.
• Front end applications eg. pure React JS, AngularJS, Mobile Apps, cannot use webhooks
directly.
• Webhooks basically are APIs implemented by API consumers but defined and used by API providers
to send notifications of events.
Websockets and SSE
How could an API server send a stream of events requested by a consumer
• Using SSE, a server can send event data to consumers.
• The server-side script that sends events using the MIME type text/event-stream. Each
notification is sent as a block of text terminated by a pair of newlines
• WebSocket is bidirectional, a full-duplex protocol used in the same scenario of client-server
communication, unlike HTTP it starts from ws:// or wss://.
javascript.info/article/server-sent-events/eventsource/
event: userconnect
data: {"username": "bobby", "time": "02:33:48"}
Communication across services
Asynchronous event-driven communication
14
Messaging
• Refactor the solution and introduce a queue between the client and the service.
• The client and the service run asynchronously
• A message broker is an intermediary through which all messages flow. A sender writes the message
to the message broker, and the message broker delivers it to the receiver
• Examples of popular open-source message brokers include the following:
• ActiveMQ (http://activemq.apache.org)
• RabbitMQ (https://www.rabbitmq.com)
• Apache Kafka (http://kafka.apache.org)
Messaging
• Refactor the solution and introduce a queue between the client and the service.
• The client and the service run asynchronously
• A message broker is an intermediary through which all messages flow. A sender writes the message
to the message broker, and the message broker delivers it to the receiver
• Examples of popular open-source message brokers include the following:
• ActiveMQ (http://activemq.apache.org)
• RabbitMQ (https://www.rabbitmq.com)
• Apache Kafka (http://kafka.apache.org)
Message Oriented Middleware
A message broker is a software that enables services and applications to communicate through messages.