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.
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.
- 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.
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.
- A browser connects to the server.
websocat "ws://localhost:3000/_ws/"
- 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"}
- A browser subscribes to a domain and channel.
{ "event": "subscribe", "domain": "http://blah.com", "channel": "test" }
- 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"}
- The browser broadcasts a message to a domain and channel.
{ "event": "broadcast", "domain": "http://blah.com", "channel": "test", "message": "Hello" }
- 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 on the command line:
npx ws-subscribe-broadcast
Running in Docker:
docker run -p 3000:3000 jasony/ws-subscribe-broadcast
npm install
npm start
docker build -t ws-subscribe-broadcast .
docker run -p 3000:3000 ws-subscribe-broadcast
You can set the following environment variables: PORT - The port the server will run on. Default is 3000.
env PORT=3001 npx start
To test from the command line, you can use websocat:
websocat "ws://localhost:3000/_ws/"
{ "event": "subscribe", "domain": "http://blah.com", "channel": "test" }
Note: You can subscribe to multiple domains and channels at once.
{ "event": "broadcast", "domain": "http://blah.com", "channel": "test", "message": "blah" }
{ "event": "unsubscribe", "domain": "http://blah.com", "channel": "test" }
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 all messages.
{ "event": "get", "domain": "http://blah.com", "channel": "test" }
Get all messages since a timestamp.
{ "event": "get_since", "domain": "http://blah.com", "channel": "test", "since": "1577836800000" }
Get all messages since a date.
{ "event": "get_since_date", "domain": "http://blah.com", "channel": "test", "since": "2020-01-01" }
Get a message by ID.
{ "event": "get_by_id", "domain": "http://blah.com", "channel": "test", "id": "644005ae9dfd5ec3247d163a" }
Get all messages since a message ID.
{ "event": "get_since_id", "domain": "http://blah.com", "channel": "test", "since_id": "644007857a34daaf6c82b942" }
Get a message by index.
{ "event": "get_by_index", "domain": "http://blah.com", "channel": "test", "index": 1 }
Get the latest message.
{ "event": "get_latest", "domain": "http://blah.com", "channel": "test" }
Displays this README.
This is the websocket endpoint. You can connect to it using a websocket client.
Get basic stats
curl "http://localhost:3000/stats/"
{
"status": "ok",
"time_running": 102,
"sockets_open": 1,
"message_count": 4
}
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"
}
This app is compatible with the VIP Go platform.
See CHANGELOG.md.
MIT