npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

events-broadcaster

v0.0.2

Published

Broadcast events through Redis PubSub

Downloads

6

Readme

Events Broadcaster

What?

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.

Why?

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:

  • listen on onMessage(callback) to receive messages from other instances
  • call maybeSubscribe(channel) when a new client joins
  • call maybeUnsubscribe(channel) when a client disconnects

with channel being your socket.io room (it also works with several rooms per client). The library will:

  • subscribe when needed (only if you aren't already)
  • unsubscribe when needed (if the last connected client disconnects: unsubscribe)

That way, each server instance will only listen to the channels/rooms having connected clients.

How?

Please see the examples/ folder for a complete working example.

API

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.