A scalable Server Sent Events broker
-
Channels
- Seperate streams of events, they are created dynamically when the first client subscribes and are deleted automatically when the last client disconnects.
-
Scalability
- Each node uses gossip protocol to discover more nodes. New nodes need only be started with the hostname of a single active node in the cluster.
- When a node recieves an event, it propagates it to the next node, appending metadata to the message to avoid event duplication
- Nodes provide their HTTP port as gossip metadata, allowing connections between nodes that are configured differently from one another.
-
EventSource
compatibility- Using JavaScript, you can use native
EventSource
class to stream events from the broker. Below is an example:
- Using JavaScript, you can use native
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);
Each node can be ran as a single binary, docker image or Kubernetes deployment.
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
The application is also available as a docker image here
docker run -d davidsbond/sse-cluster start
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 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 |