![]() ![]() If the server accepts the request, both parties can use the socket to send each other messages. The client initiates the handshake with a request to upgrade the protocol from HTTP to WebSocket. In HTTP/1.1, to start using the WebSocket protocol, the client and server need to go through what’s called the WebSocket handshake. Like HTTP, the WebSocket protocol is built on top of TCP. In contrast to SSE, which works on top of HTTP, WebSocket is an entirely different protocol. Importantly, it enables two parties to exchange messages with each other at the same time. WebSocket is a bidirectional, full-duplex protocol for communication between a client and a server over the web. If you want to either stream binary data to the client or have bidirectional communication with the client, you’ll be better suited to use a more complex technology such as WebSocket. While you can technically implement something bidirectional like a chat application through SSE, it’s not ideal for this use case since you’ll need two separate streams (client -> server, server -> client), state of which will somehow need to be synchronized. SSE is the best option when you have continuously updating real-time text data that you need to display to the client (logging, stock data, live feed). Major cloud vendors’ regular offerings also easily support server-sent events. Need to serve more users with server-sent events? Add more servers and a load balancer in the middle, and you’re done. Its simplicity also makes it easy to scale. It also supports automatic reconnection out-of-box, which can be very useful. SSE works on top of HTTP, meaning that you can utilize various HTTP conveniences such as easy dealing with proxies and compression. While the direction requirement might seem limiting, it also makes the technology quite simple and efficient. ![]() Meanwhile, if the client wants to send a message to the server, it can send a different request, but it will be separate from the stream and won’t automatically share its state. Messaging in SSE is unidirectional-only the server can send the messages through the stream. The server can then send a continuous stream of messages (events) to that client. With SSE, the client uses JavaScript’s EventSource interface to subscribe to a stream on a server. There are two popular ways of streaming messages from a server to a client: server-sent events (SSE) and WebSocket. The first two are commonly used for communication between the client and the server, while the other three are more commonly used for communicating between different backend services, such as devices or microservices. They are the gold standard in their areas, so if your use case requires streaming data, you should definitely check them out. This article covers five APIs/protocols that are reliable, scalable, and commonly supported by cloud vendors. ![]() Streaming APIs use a push-based model, where data is sent to the client once it’s available instead of being sent on request. But these are crutches, trying to introduce server-side updates into a model that doesn’t really support them.Ī much more efficient solution is to use a streaming API instead. The server can either repeatedly ask for updates (short polling) or ask for an update and wait until the response arrives (long polling). There are solutions to overcome this weakness. ![]() This makes it hard for the server to take the initiative and push updates to the client. Clients request information, and the server responds to their requests. In a REST architecture, there are two parties: clients and the server. REST is the most prevalent type of API on the internet, but it’s not fit for every use case. ![]()
0 Comments
Leave a Reply. |