sse-cluster
A scalable Server Sent Events broker
Features
const channel = 'my-channel'
// Connect to a single node, or to a load balancer in-front
// of many nodes
const es = new EventSource(`https://my-sse-cluster:8080/channel/${channel}`)
// Handle the stream generically, all events will trigger this method
es.onmessage = (e) => {
const newElement = document.createElement('li');
const eventList = document.getElementById('list');
newElement.innerHTML = `message: ${e.data}`;
eventList.appendChild(newElement);
}
// Handle specific event types, this method is invoked for 'ping' events.
es.addEventListener('ping', (e) => {
const newElement = document.createElement('li');
const obj = JSON.parse(e.data);
newElement.innerHTML = `ping at ${obj.time}`;
eventList.appendChild(newElement);
}, false);
Installation
Each node can be ran as a single binary, docker image or Kubernetes deployment.
Installing from source
This section assumes you have go 1.11+ installed.
# download the source code
go get github.com/davidsbond/sse-cluster
# install the binary
go install github.com/davidsbond/sse-cluster
# start a node
sse-cluster start
Running with docker
The application is also available as a docker image here
docker run -d davidsbond/sse-cluster start
Installing with helm
This repository also contains a helm chart for deploying to Kubernetes clusters.
helm install my-sse-cluster ./helm/sse-cluster/
Upon success, a StatefulSet
and HorizontalPodAutoscaler
will be created in your Kubernetes cluster that will manage the number of sse-brokering nodes. A headless service will also be created to allow name resolution of individual pods. Without this, the sse broker nodes will not be able to communicate.
Configuration
Configuration is provided to a node either via environment variables or command-line arguments. Most aspects of the broker can be controlled via these configuration values.
Argument |
Environment Variable |
Description |
Default |
gossip.port |
GOSSIP_PORT |
The port to use for communications via gossip protocol |
N/A |
gossip.hosts |
GOSSIP_HOSTS |
The initial hosts the node should connect to, should be a comma-seperated string of hosts |
N/A |
gossip.secretKey |
GOSSIP_SECRET_KEY |
The key used to initialize the primary encryption key in a keyring |
N/A |
http.client.timeout |
HTTP_CLIENT_TIMEOUT |
The time limit for HTTP requests made by the client |
10s |
http.server.port |
HTTP_SERVER_PORT |
The port to use for listening to HTTP requests |
8080 |
http.server.cors.enabled |
HTTP_SERVER_ENABLE_CORS |
If set, allows cross-origin requests on HTTP endpoints |
false |