sse-cluster

command module
v0.0.0-...-525d4e8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 4 Imported by: 0

README

sse-cluster

CircleCI Docker Cloud Build Status Coverage Status Go Report Card GoDoc

A scalable Server Sent Events broker

Features

  • 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:
  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

Documentation

Overview

Package main contains the entrypoint of the application

Directories

Path Synopsis
Package broker contains the broker implementation.
Package broker contains the broker implementation.
Package cmd contains methods for generating the cli commands used to manage a broker
Package cmd contains methods for generating the cli commands used to manage a broker
Package handler contains types for interacting with the broker implementation via HTTP.
Package handler contains types for interacting with the broker implementation via HTTP.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL