Here is another one of those websocket brokers that allow for pubsub message exchange. Unlike others you might see elsewhere, this one can augument your existign http server, i.e. Express.
Available as NPM package yaws-pubsub
After client connects to a websocket (with optional external authentication), they can issue commands by sending these JSON messages:
{
"command" : "subscribe",
"channel" : "string-name"
}
{
"command" : "unsubscribe",
"channel" : "string-name"
}
{
"command" : "publish",
"channel" : "string-name",
"payload" : { some - object}
}
const server = http.createServer();
start(server);
server.listen(PORT);
Also see ./tests/TestUtils.ts
pass instance of Express to createServer:
import express, { Request, Response } from 'express';
import http from 'http';
import { start } from 'yaws-pubsub/Server';
const app = express();
const port = 3000;
app.get('/hello', (req: Request, res: Response) => {
res.send('Hello from Express!');
});
const server = http.createServer(app);
start(server);
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Add authenticate function to start() call:
authenticate?: (socket: WebSocket, request: http.IncomingMessage) => boolean
as in:
start(server, (socket, request) => {
return request.url?.indexOf('good') !== -1;
});