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

ws-subscribe-broadcast

v0.2.6

Published

Simple websocket server lets you subscribe and broadcast

Downloads

9

Readme

WS-Subscribe-Broadcast

This simple websocket server lets you subscribe to a domain and channel, and then lets you send and receive all broadcasts for that domain and channel.

Background

This Websocket Server was developed for a Daily Maverick Mavengine project that required multiple users' browsers to remain in sync with each other, either when a user made a change, or when the server made a change. Instead of building a specific solution for that project, we decided to build a generic subscribe-broadcast server that could be used for any project that required this functionality.

Features

  • Multiple sites can use the server by using their own "Domain".
  • Multiple apps can use the server by using their own "Channel".
  • A web browser can subscribe to one or more domains and channels.
  • A browser can send a message that will be broadcast to all the other subscribers on the channel.
  • A server can also send a message for broadcast through a POST request.
  • There is limited history (currently 100 messages) that can be retrieved by a browser.

Security

The system is not secure. Anyone can subscribe to any domain and channel.

We suggest not posting any data over the websocket. We typically use it to announce that something has changed, but no data as to what exactly has changed. The browser then makes a request to the server to get the latest data.

Communication model

Communication model

  1. A browser connects to the server.
websocat "ws://localhost:3000/_ws/"
  1. The browser gets a response that it has successfully connected. This inculdes a unique ID for the browser.
{"event":"connected","message":"Connected to server","uid":"6441066c31e24e0b1a511980"}
  1. A browser subscribes to a domain and channel.
{ "event": "subscribe", "domain": "http://blah.com", "channel": "test" }
  1. The browser gets a response that it has successfully subscribed.
{"event":"subscribed","message":"Subscribed to http://blah.com/test","domain":"http://blah.com","channel":"test","uid":"6441066c31e24e0b1a511980"}
  1. The browser broadcasts a message to a domain and channel.
{ "event": "broadcast", "domain": "http://blah.com", "channel": "test", "message": "Hello" }
  1. The message is broadcast to all the subscribers to that domain and channel, but not to the browser that sent the message.
{"_id":"64410697a61e3f1cc6d357e0","data":"Hello","timestamp":"2023-04-20T09:32:07.977Z","sender":"6441066c31e24e0b1a511980","domain":"http://blah.com","channel":"test","event":"broadcast"}

Running

Quick start

Running on the command line:

npx ws-subscribe-broadcast

Running in Docker:

docker run -p 3000:3000 jasony/ws-subscribe-broadcast

Start the server

npm install
npm start

Building with Docker

docker build -t ws-subscribe-broadcast .
docker run -p 3000:3000 ws-subscribe-broadcast

Configuration

You can set the following environment variables: PORT - The port the server will run on. Default is 3000.

env PORT=3001 npx start

Usage

To test from the command line, you can use websocat:

websocat "ws://localhost:3000/_ws/"

Subscribe to a domain and channel

{ "event": "subscribe", "domain": "http://blah.com", "channel": "test" }

Note: You can subscribe to multiple domains and channels at once.

Send a broadcast

{ "event": "broadcast", "domain": "http://blah.com", "channel": "test", "message": "blah" }

Unsubscribe

{ "event": "unsubscribe", "domain": "http://blah.com", "channel": "test" }

Broadcast a message from outside a websocket

You can broadcast a message to a domain and channel by sending a POST request to the server, using the endpoint /broadcast.

curl -X POST -d "domain=http://blah.com&channel=test&message=Hello" "http://localhost:3000/broadcast"

Get historical messages

get

Get all messages.

{ "event": "get", "domain": "http://blah.com", "channel": "test" }

get_since

Get all messages since a timestamp.

{ "event": "get_since", "domain": "http://blah.com", "channel": "test", "since": "1577836800000" }

get_since_date

Get all messages since a date.

{ "event": "get_since_date", "domain": "http://blah.com", "channel": "test", "since": "2020-01-01" }

get_by_id

Get a message by ID.

{ "event": "get_by_id", "domain": "http://blah.com", "channel": "test", "id": "644005ae9dfd5ec3247d163a" }

get_since_id

Get all messages since a message ID.

{ "event": "get_since_id", "domain": "http://blah.com", "channel": "test", "since_id": "644007857a34daaf6c82b942" }

get_by_index

Get a message by index.

{ "event": "get_by_index", "domain": "http://blah.com", "channel": "test", "index": 1 }

get_latest

Get the latest message.

{ "event": "get_latest", "domain": "http://blah.com", "channel": "test" }

HTTP endpoints

GET /

Displays this README.

UPGRADE /_ws or /socket.io or /ws or /websocket

This is the websocket endpoint. You can connect to it using a websocket client.

GET /stats/

Get basic stats

curl "http://localhost:3000/stats/"
{
    "status": "ok",
    "time_running": 102,
    "sockets_open": 1,
    "message_count": 4
}

POST /broadcast

Broadcast a message to a domain and channel.

curl -X POST -d "domain=http://blah.com&channel=test&message=Hello" "http://localhost:3000/broadcast"
{
    "event": "broadcast",
    "data": {
        "_id": "6447eb6e77fb1a221c1ad75e",
        "data": "Hello",
        "timestamp": "2023-04-25T15:02:06.744Z",
        "sender": "::ffff:127.0.0.1",
        "domain": "http://blah.com",
        "channel": "test"
    },
    "domain": "http://blah.com",
    "channel": "test",
    "status": "ok"
}

Wordpress VIP

This app is compatible with the VIP Go platform.

Changelog

See CHANGELOG.md.

License

MIT