From the client, we create a new websocket connection as below The websocket comes with
- a property to check the connection status - readyState,
- functions like send() and close()
- event callbacks like onclose(), onerror(), onmessage(), onopen() etc...
On the server, the websocket will be handled in a specific port of the server (configurable) and the server will be listening for messages/data from the client. Similar to the client, WebSocketServer comes with functions like "send()", "close()", event callbacks for "connection", "error", "close", etc... Pretty much similar, except a server can connect and interact with multiple connections all at once. In order to acheive this, the server must be aware of all the users that are active and in session. So usually once a user logs in the app, the client registers with the server, and the server responds with the available active users list during the time of registering. Also simultaneously all the registered active users gets notified about the new user. And when the user leaves the app, the client sends a unregister event, so the server stops sending events/messages to that client. Once unregistered successfully, Websocket.close() is called and the connection is disconnected.
FYI: Whatsapp uses Websocket with MQTT(sub-protocol) for real time messaging What is MQTT?