SSE API using in WSO2 APIM
What are Server Sent APIs (SSE APIs) ?
Server-Sent Events (SSE) is a standard API type that allows a server to push real-time updates to a client over an HTTP connection.
After the initial HTTP connection gets built up, the server sends the data through multiple events. These events are based on a topic. For example REST API using GET, POST, UPDATE and DELETE methods in the APIs to perform the task which is client needed.
Like REST, when a topic is GET, those events based on the GET topic will send the data of the particular data load from the server.
Features of SSE
- Unidirectional communication: Data sending only from server to client.
- Lightweight protocol: Building a single HTTP connection, making it simple for data sending server to client.
- Text based format: Sending the data in a plain text format.
Common use cases
- Real time notification systems.
- Chat applications.
- Stock market price updates.
Let’s look at some comparisons of other streaming methods like web socket and web hooks.
SSE vs Web Socket
- Web socket is a special kind of web protocol used to build real time communication systems.but SSE can be used in any kind of client-server communication.websocket transmits data in full duplex mode but sse works in half duplex mode.
- SSEs are a valuable tool as they provide a standardized and efficient way to implement real-time updates for unidirectional use cases.
- They offer benefits such as instant updates, reduced network traffic and server load, simplified client-side implementation, and compatibility with existing web infrastructure. By leveraging SSEs, developers can enhance the user experience of their realtime apps and provide users with live and up-to-date information more efficiently and scalable.
SSE vs Web Hooks
- Web hooks are communicated between server to server. server makes an HTTP POST request to another server (or service) when an event occurs.
- The sending server pushes the request to a specific endpoint(URL) of the receiving server.
- Lightweight protocol.
- One way communication method.
- Used in notification services when there are server to server updates needed.
- SSE is based on server to client event sending communication. In webhooks, two servers communicate and one server makes a HTTP POST method to the receiving server.
Envoy proxy configurations using for SSE
1.Timeout configuration
When upstream servers send responses to the downstream client in a single connection, that connection should persist. For that we need to set the time_out fields set to 0.
In the above shown envoy yaml configuration file, timeout=0 will keep a persistent upstream server — downstream client connection.
Also even when the connection is in an idle state(Not sending data from server to client) idel_timeout=0 will keep the connection indefinitely.
Chat streaming application with envoy proxy : SSE_chat_envoy-proxy
2. Rate limiting
Rate limiting is a special feature that is used to limit the client requests over a given period of time. rate limiting will help to reduce request response latency and be helpful to keep client-server communication in a more persistent way.
The above configuration shows stream service rate limited with 100 requests per minute and chat service rate limited with 10 requests per second.
SSE API using in WSO2 — APIM
In WSO2 APIM, SSE streaming API creation, publishing and invoking is based on an AsyncAPI definition file.
One of the major use cases of SSE APIs is a real time data updates scenario.here is an API definition file for a real time news updating website.
Below shown AsyncAPI definition file will create the API
Inserting the relevant basepaths and endpoints mapping to the relevant resources in the news website and the server will send data through those channels to the clients.
SSE_AsyncAPI.yaml API definition file: SSE-AsyncAPI definition