In this guide, we will use this - https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
WebSocket object provides the API for creating and managing a WebSocket connection.
To create a new WebSocket connection, use the WebSocket constructor:
const socket = new WebSocket('ws://example.com/socketserver');
WebSocket connections have four main event handlers:
Fired when the connection is established.
socket.onopen = (event) => {
console.log('Connection opened');
};
Fired when a message is received from the server.
socket.onmessage = (event) => {
console.log('Received:', event.data);
};
Fired when the connection is closed.
socket.onclose = (event) => {
console.log('Connection closed');
};
Fired when an error occurs.
socket.onerror = (error) => {
console.error('WebSocket Error:', error);
};