From d58e537b805ea6c81147249faac6b745e2083174 Mon Sep 17 00:00:00 2001 From: FrancelWebdev Date: Tue, 7 Nov 2023 14:22:19 +0100 Subject: [PATCH] Create websocket-with-ws --- docs/3rd-party-lib/websocket-with-ws | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 docs/3rd-party-lib/websocket-with-ws diff --git a/docs/3rd-party-lib/websocket-with-ws b/docs/3rd-party-lib/websocket-with-ws new file mode 100644 index 0000000..ef4036d --- /dev/null +++ b/docs/3rd-party-lib/websocket-with-ws @@ -0,0 +1,36 @@ +# Websocket with ws library + +https://github.com/websockets/ws + +### Import + +```ts +// Node or Bun +import { WebSocketServer } from 'ws' + +``` + +### Usage + +```ts +// Node or Bun +const app = nhttp(); + +const server = createServer(app.handle as any); + +const wss = new WebSocketServer({ server }); + +wss.on("connection", function connection(ws) { + console.log('ws -> client connected'); + + ws.on("error", function error(error) { + console.error('error', error) + }); + + ws.on("message", function message(message) { + console.log("received: %s", message); + }); + + ws.send("something"); +}); +```