Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
events-broadcaster
Advanced tools
Library using Redis PubSub to scale a websocket server (such as socket.io, but not exclusively) to more than one instance.
You can check the code in src/index.js, it's pretty straightforward.
The typical example is a chat application using a socket.io server having a lot of rooms. If you want to scale this server to more than one instance, you won't be able to directly broadcast messages to all connected clients, because the broadcasting instance won't know about the connected clients on the other instances.
So, you need some sort of mechanism to broadcast events to all the other instances. This is what this library does using Redis PubSub, you have to:
onMessage(callback)
to receive messages from other instancesmaybeSubscribe(channel)
when a new client joinsmaybeUnsubscribe(channel)
when a client disconnectswith channel
being your socket.io room (it also works with several rooms per client).
The library will:
That way, each server instance will only listen to the channels/rooms having connected clients.
Please see the examples/ folder for a complete working example.
maybeSubscribe(channel: string): Promise<void>
Subscribe to the Redis channel. When subscribed, the onMessage(f)
callback will be called when receiving messages from this channel.
Internally, we keep a count of how many subscribers we have for each channel. We don't do anything if we are already subscribing to this channel. See an example.
maybeUnsubscribe(channel): Promise<void>
Unsubscribe to the Redis channel if needed. As said in maybeSubscribe(channel)
, we keep track of how many subscribers we have for each channel. We'll unsubscribe only if the subscriber count for the channel is 0. See an example.
publish(channel, message): Promise<void>
Send a message through the Redis channel via PubSub. Subscribed server instances (including this one) will receive the message via the onMessage(f)
callback function. See an example.
onMessage(f: (channel, message) => any): void
Listen to new messages and receive the ones sent with the publish(channel, message)
function. See an example.
FAQs
Broadcast events through Redis PubSub
We found that events-broadcaster demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.