Web applications are basically pull-type. In other words, when a client accesses the application, an event occurs on the server side, and the server responds to the event.
However, in some cases, the server wants to send a message to the client. This article introduces a collection of technologies that can be used for sending messages from the server side to the client.
Websocket is the first option for sending and receiving messages between the server and the client. Messages are sent and received in real-time since the socket is left open and messages are sent to each other.
Since only text messages can be sent, it is often used for chats or server process completion notifications.
SSE(Server-Sent Events) can be used like WebSockets, but is characterized by the fact that it is a one-way communication from server to client, and cannot be used for messages from client to server like WebSockets.
Therefore, it is often used to notify asynchronous processing on the server. Chat-like functions can be realized, but there seems to be little merit in using SSE for this purpose.
Unlike WebSocket, WebRTC can handle video and audio. It can also be used for P2P mutual transmission and in the form of a central server (a server is required during the initial connection).
WebRTC provides a DataChannel, which can handle text (and other data). The DataChannel uses UDP, not TCP.
Long Polling will be a technology used to achieve real-time communication without standard technologies such as WebSocket. The connection between the client and server is maintained for an extended period of time, during which time data is sent. When the connection is broken, the request is immediately executed again.
The advantage is that it can be used even by clients that do not support WebSockets, but there is a disadvantage in that it consumes a large number of connections on the server side.
Web push notifications can receive events in the web browser when they are opened. You could write a process that uses it to retrieve data from the client.
There is some work involved in implementing this, such as requiring the client’s permission to send the data, and saving the key obtained from the browser.
The web site does not have to be open, but the browser must be running.
Webhooks are also a popular technology for implementing such functions, as they execute requests to a server rather than a client. Webhooks are used for server-to-server communication.
When a server needs to know that something has changed in its data, it will typically make frequent requests, or use a connection maintenance technology such as WebSockets. With Webhook, however, you will only receive notifications from the server when data has been updated, and only when it has been updated.
By sending messages from the server, it will be possible to notify the status in real-time and reflect the status on the screen. It can be used to realize collaboration functions (Collaboration editing) for business applications and exchanging messages.
Hexabase supports sending messages via WebSocket and SSE. Please try it in your application.
I hope you learn something new. And don’t forget to share the article!