Using a WebSocket connection
As mentioned, WebSockets is a powerful technology that enables bidirectional communication between client and server. In this section, we are going to use a WebSocket connection to connect with our server to obtain and send messages. But before we do that, it is essential to understand the alternatives and the rationale behind choosing WebSockets for our messaging app.
Why WebSockets?
There are several options for enabling real-time communication between clients and servers, including the following:
- Long polling: This is when the client sends a request to the server, and the server holds the request until new data is available. Once the server responds with the new data, the client sends another request, and the process repeats.
- Server-Sent Events (SSE): SSE is a unidirectional communication method where the server pushes updates to the client over a single HTTP connection.
- Real-time cloud databases (for example, Firebase Firestore...