
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
pubsub-ws is a simple Node JS library for communicating over websockets using the publish-subscribe pattern.
pubsub-ws is a simple Node JS library for communicating over websockets using the publish-subscribe pattern.
It is written in Typescript and uses the ws package for websockets. The author's primary use case was to push real time updates to a web UI for features such as graphs and progress bars. However, it could be used anywhere that pubsub over websockets is needed.
npm i -s pubsub-ws
import http from 'http';
import WebSocket from 'ws';
import { createBroker } from 'pubsub-ws';
// ----- Set up the websocket server and pubsub broker -----
const server = http.createServer(); // https server is also supported
The second argument to createBroker is the getChannel function, which is called each time a websocket upgrade request is received. getChannel receives the http request and expects back a channel. The websocket is subscribed to this channel. We can subscribe all websockets to the same channel (as in this example) or use data in the request to intelligently subscribe different websockets to different channels. The latter is shown in the authentication example, further down in the readme.
const broker = createBroker(server, (request) => {
return Promise.resolve('myChannel');
});
server.listen(7123);
const ws = new WebSocket('ws://localhost:7123');
ws.on('open', () => console.log('ws open'));
ws.on('message', (data) => console.log(`received message: ${data}`));
setInterval(() => {
broker.publish('myChannel', 'test data');
}, 2000);
The getChannel function (called each time a websocket upgrade request is received) can be used to authenticate clients. Reject the returned Promise if the request is unauthenticated. Below is a typescript example using session authentication with the express-session middleware to parse a passport js session.
import session from 'express-session';
import { createBroker } from 'pubsub-ws';
const sessionParser = session({
// your session options here
});
const authenticateAndGetChannel = (req: any) => {
return new Promise<string>((resolve, reject) => {
sessionParser(req, {} as any, async () => {
if (!req.session?.passport?.user) {
logger.info('Unauthenticated websocket request');
reject();
} else {
resolve(user); // in this example, each user subscribes to its own channel
}
});
});
}
const broker = createBroker(server, authenticateAndGetChannel));
express-shared-port - Websockets, express APIs, and express static files all on the same port (server). Includes an example of connecting websockets from a frontend html page.
FAQs
pubsub-ws is a simple Node JS library for communicating over websockets using the publish-subscribe pattern.
We found that pubsub-ws demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.