const socket = new WebSocket('ws://localhost:8080'); socket.addEventListener('open', function (event) { socket.send('Hello Server!'); }); socket.addEventListener('message', function (event) { console.log('Message from server: ', event.data); }); socket.addEventListener('close', function (event) { console.log('Server closed the connection'); });In this example, a WebSocket client establishes a connection with the server at ws://localhost:8080. Once the connection is established, the client sends a message to the server and waits for a response. The server can send a message to the client at any time, which is handled by the message event listener. This code example uses the native WebSocket API provided by modern browsers. No package library is needed. However, there are many WebSocket libraries available that offer additional features and cross-browser support, such as socket.io, SockJS, and WS.