ClusterWS-uWS
Modified version of uWebSockets
This module is modified version of the uWebSockets with some minor tweaks in C++ code and complete rewrite of JS code to TS.
Please consider to support ClusterWS development:
Installation
npm i clusterws-uws
Server example
uWebSockets node was designed to mimic node js ws module there are some things which are not available in uWebSockets.
// use WebSocketServer to create serverconst WebSocketServer = ; // Create websocket server const server = port: 3000 { console}; // Accept ws connectionsserver; // Start auto ping (second parameter is type of ping `false` is low level)// use `false` most of the time except if you want to track ping pong on the client side // which does not have onping & onpong methods (like browser websocket)// check Handle AutoLevelPing In Browser Example part below// event if you use app level ping server onPong will be calledserver // broadcast to all connected clients// message: string | binary (node buffer)// options?: { binary: true | false }server // destroy or close serverserver
Client example
// Client part is pretty much the same as in server// use WebSocket to create clientconst WebSocket = ; const socket = 'ws://url:port'; // emitted when websocket is connectedsocket // emitted when receive new messagesocket; // emitted when error happenssocket // emitted on close websocketsocket // emitted when get ping from the server (if you send)socket // emitted when get pong from the server (if you send)socket socket // manually send ping to the server socket // send message to the server binary | string socket // destroy connection socket // close connection
Handle AppLevelPing In Browser Example
This is just an example of handling app level ping pong from the client side which does not have onping
and onpong
methods available
Note if your clients have onping
and onpong
methods (or similar) do not send appLevel
ping from the server as it requires more work.
socketbinaryType = 'arraybuffer' // Do not forget to set to `arraybuffer`socket { if typeof messagedata !== 'string' let buffer = messagedata; if buffer0 === 57 // output should be an array with one bit and [0] is === 65 return socket; // process with your binary data // process with your string data}