ws library is the most popular one and it is fully compatible with the Official WebSocket protocal, however, if you want to use ws as client in nodejs, a wrapper like isomorphic-ws is necessary, which is also used in the examples.socket.io library has its own features like load balance and auto-reconnection functions although it is not compatible with the Official WebSocket protocal and you need to install it both in server side and client side. In client side, you need socket.io-client library instead of socket.io.message event with hello client to the clientmessage event, it send an response event with response + randmon number to the serverresponse event, it send an end event with the content it received.end event, it closes the connection after 3s delay.npm run start to start it. There is another ws server wrapped in a httpServer in the server folder named server2.ts, run npm run start2 to start it, to make the example simple, the two servers run on the same port 18000(the http server listens port 3000).client folder contains:client.html, open with a brawser to startws client with native ws library, run npm run startws client compatible with native WebSocket protocol, run npm run start2ws and native WebSocket, in ws library:message in the two protocols are also different, in the ws library it is WebSocket.data while in the native WebSocket protocol is WebSocket.MessageEvent, the latter structure is similar as below as the former contains only the data part.socket.io server and client implemented with nodejs. Install and npm run start in server and client folder to start each. There is another socket.io server wrapped in a httpServer in the server folder named server2.ts, run npm run start2 to start it, to make the example simple, the two servers run on the same port 18000index.html file which implemented a brawser client:socket.io.js file (a socket.io.js.map may also needed, both files can be found on socket.io library client-dist folder) which is replied by the html file.socket.io.js v2 file to connect v3 server may cause 400 error.socket.io v3 uses CORS, simply open the index.html file in a brawser cannot visit the server. We installed http-server here (which uses index.html as default page), and config the CORS option in the server (see below), run npm run startweb and visit http://localhost:8081 in a brawser to connect to the server.socket.io v3 and not compatible with socket.io. See Socket.io Document for the detailed differences between the two version. Some import feature are as follows:socket.io v3 is rewritten by TypeScript, there is no need to import @types/socket.io or @types/socket.io-clientlibrary to use socket.io in TypeSctipt, import the above libraries in your code may cause mistake.CORS needs to be explicitly announced as follows:socket.io support autoreconnection mechanism which is enabled by default, if you want to test the function, just comment the setTimeout function in client, stop and start the server again, the client will reconnect to the server automatically.socket.io support message acknowledgement mechanism, the response event in this example added it, the server will give an acknowledgement message through callback function when it receives a response message.socket.io event are highly customised ,you need to decide and encode/decode the content and avoid possible mistakes.Nestjs framework using ws library. As Nestjs is a server framework itself, a html file is added with nestjs serve Static function and MVC service. the communication interfaces in this example is similar to 1. nodejs ws example, so the servers and clients can connect with each other. However, for test purpose, the functions a modifed in this example so as to keep the client keep connected with the server until there is an order to terminate:end message when it received a response message, instead, it sends a same response message to the client.response message listener is added to the client, it will print the message it received.terminate message will be send from the client trigged by a http request sendTerminate, the server will send the old end message when it received the terminate message.Get http://localhost:3001/ws-client/getdata to send response and update the data it received. -Get http://localhost:3001/ws-client/sendTerminate 用to send terminatemessage,This will also terminate the client app, you need restart it againclient.http file is created in client folder to test http requests, which can be used by vscode plugin REST Client.cors configuration when use ws library.server and client run npm run start to start them and see the result.http://localhost:3001, to see and update data in console and in the page.1. nodejs ws example servers and clients, you need to pay attention to the API difference.Nestjs framework using socketio library. The program structure and API interface are exactly the same as example 3. Except for some important NOTES:socket.io version 3.x and 2.x are incompatible, they cannot work together, which means the server and client shall of the same major version.Nestjs built-in socket.io library @nestjs/platform-io was written with socket.io version 2, so it is incampatible with a version 3 client.Nestjs author promised to update socket-io on nestjs 8 ,however, if you want to use socket.io v3 in nestjs, a temporary approach is introduced in nestjs issue 5676, use the socket-io.adapter in the issue (the name line under cookie shall be removed) instead of the official one. This is the way we used here.@WebSocketGateway() decorator mechanism, you will not visit the server if the port parameter (for example @WebSocketGateway(18000)) is different from the server http port (for example app.listen(3000)) even if cors is enabled. You shold leave the @WebSocketGateway() decorator blank and use the client to visit the same http port.Posted May 15, 2025
Developed Node.js WebSocket examples repository.
0
0