1.2.7 • Published 10 months ago

@prsm/express-keepalive-ws v1.2.7

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
10 months ago

@prsm/express-keepalive-ws

This is a middleware that creates and exposes a KeepAliveServer instance (see prsm/keepalive-ws.

import express from "express";
import createWss, { type WSContext } from "@prsm/express-keepalive-ws";

const app = express();
const server = createServer(app);

const { middleware: ws, wss } = createWss({ /** ... */ });

app.use(ws);

// as a middleware:
app.use("/ws", async (req, res) => {
  if (req.ws) { // <-- req.ws will be defined if the request is a WebSocket request
    const ws = await req.ws(); // handle the upgrade and receive the client WebSocket
    ws.send("Hello WS!"); // send a message to the client
  } else {
    res.send("Hello HTTP!");
  }
});

// as a command server:
wss.registerCommand("echo", (c: WSContext) => {
  const { payload } = c;
  return `echo: ${payload}`;
});

Client-side usage (more at https://github.com/node-prism/keepalive-ws):

import { KeepAliveClient } from "@prsm/keepalive-ws/client";

const opts = { shouldReconnect: true };
const ws = new KeepAliveClient("ws://localhost:PORT", opts);

const echo = await ws.command("echo", "hello!");
console.log(echo); // "echo: hello!"
1.2.7

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.4

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.0

11 months ago

1.1.0

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago