A real-time message bus server and library written in Go with strong consistency and reliability guarantees.
Go to file
James Mills b1d7a8dad4
Add grafana dashboard
2018-05-02 02:40:23 -07:00
.github/ISSUE_TEMPLATE Update issue templates 2018-05-02 01:37:44 -07:00
client Switched to using the github.com/gorilla/websocket library 2018-05-01 23:19:04 -07:00
cmd Fixed tests 2018-05-02 01:24:30 -07:00
examples Fixed tests 2018-05-02 01:24:30 -07:00
grafana Add grafana dashboard 2018-05-02 02:40:23 -07:00
vendor Updated 3rd-party vendored packages 2018-05-02 00:43:09 -07:00
.dockerignore Add build system and version info 2018-03-25 14:37:32 -07:00
.gitignore Fixed tests, Added Drone CI config and Docker Stackfile 2018-03-25 15:03:18 -07:00
.gitmodules Updated 3rd-party vendored packages 2018-05-02 00:43:09 -07:00
.travis.yml Add Travis CI and badges 2017-08-10 00:38:53 -07:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md (#4) 2018-05-02 01:36:30 -07:00
CONTRIBUTING.md Create CONTRIBUTING.md 2018-05-02 01:35:43 -07:00
Dockerfile Fixed Dockerfile image building 2018-03-25 15:23:52 -07:00
LICENSE Add LICENSE 2017-08-07 01:16:28 -07:00
Makefile Add more tests and bench target 2018-04-06 23:34:51 -07:00
PULL_REQUEST_TEMPLATE.md Create PULL_REQUEST_TEMPLATE.md 2018-05-02 01:39:27 -07:00
README.md Fixed image layers badge 2018-04-06 23:38:57 -07:00
docker-compose.yml Fixed tests, Added Drone CI config and Docker Stackfile 2018-03-25 15:03:18 -07:00
metrics.go Added basic metrics 2018-05-02 00:41:14 -07:00
msgbus.go Fixed tests 2018-05-02 01:24:30 -07:00
msgbus_test.go Add more tests and bench target 2018-04-06 23:34:51 -07:00
queue.go Initial Commit 2017-06-03 16:16:17 +01:00
queue_test.go Initial Commit 2017-06-03 16:16:17 +01:00
version.go Make version display consistent 2018-03-25 14:50:02 -07:00
version_test.go Fixed tests, Added Drone CI config and Docker Stackfile 2018-03-25 15:03:18 -07:00

msgbus

Build Status CodeCov Go Report Card GoDoc

A real-time message bus server and library written in Go with strong consistency and reliability guarantees.

(eventual goals of distributed high-availability and sharding)

Install

$ go install github.com/prologic/msgbus/...

Usage (library)

Install the package into your project:

$ go get github.com/prologic/msgbus

Use the MessageBus type either directly:

package main

import (
    "log"

    "github.com/prologic/msgbus"
)

func main() {
    m := msgbus.NewMessageBus()
    m.Put("foo", m.NewMessage([]byte("Hello World!")))

    msg, ok := m.Get("foo")
    if !ok {
        log.Printf("No more messages in queue: foo")
    } else {
        log.Printf
	    "Received message: id=%s topic=%s payload=%s",
	    msg.ID, msg.Topic, msg.Payload,
	)
    }
}

Running this example should yield something like this:

$ go run examples/hello.go
2017/08/09 03:01:54 [msgbus] PUT id=0 topic=foo payload=Hello World!
2017/08/09 03:01:54 [msgbus] NotifyAll id=0 topic=foo payload=Hello World!
2017/08/09 03:01:54 [msgbus] GET topic=foo
2017/08/09 03:01:54 Received message: id=%!s(uint64=0) topic=foo payload=Hello World!

See the godoc for further documentation and other examples.

Usage (tool)

Run the message bus daemon/server:

$ msgbusd
2017/08/07 01:11:16 [msgbus] Subscribe id=[::1]:55341 topic=foo
2017/08/07 01:11:22 [msgbus] PUT id=0 topic=foo payload=hi
2017/08/07 01:11:22 [msgbus] NotifyAll id=0 topic=foo payload=hi
2017/08/07 01:11:26 [msgbus] PUT id=1 topic=foo payload=bye
2017/08/07 01:11:26 [msgbus] NotifyAll id=1 topic=foo payload=bye
2017/08/07 01:11:33 [msgbus] GET topic=foo
2017/08/07 01:11:33 [msgbus] GET topic=foo
2017/08/07 01:11:33 [msgbus] GET topic=foo

Subscribe to a topic using the message bus client:

$ msgbus sub foo
2017/08/07 01:11:22 [msgbus] received message: id=0 topic=foo payload=hi
2017/08/07 01:11:26 [msgbus] received message: id=1 topic=foo payload=bye

Send a few messages with the message bus client:

$ msgbus pub foo hi
$ msgbus pub foo bye

You can also manually pull messages using the client:

$ msgbus pull foo
2017/08/07 01:11:33 [msgbus] received message: id=0 topic=foo payload=hi
2017/08/07 01:11:33 [msgbus] received message: id=1 topic=foo payload=bye

This is slightly different from a listening subscriber (using websockets) where messages are pulled directly.

Usage (HTTP)

Run the message bus daemon/server:

$ msgbusd
2018/03/25 13:21:18 msgbusd listening on :8000

Send a message with using curl:

$ curl -q -o - -X PUT -d '{"message": "hello"}' http://localhost:8000/hello

Pull the messages off the "hello" queue using curl:

$ curl -q -o - http://localhost:8000/hello
{"id":0,"topic":{"name":"hello","ttl":60000000000,"seq":1,"created":"2018-03-25T13:18:38.732437-07:00"},"payload":"eyJtZXNzYWdlIjogImhlbGxvIn0=","created":"2018-03-25T13:18:38.732465-07:00"}

Decode the payload:

$ echo 'eyJtZXNzYWdlIjogImhlbGxvIn0=' | base64 -d
{"message": "hello"}

API

GET /

List all known topics/queues.

Example:

$ curl -q -o - http://localhost:8000/
hello

POST|PUT /

Post a new message to the queue named by <topic>.

NB: Either POST or PUT methods can be used here.

Example:

$ curl -q -o - -X PUT -d '{"message": "hello"}' http://localhost:8000/hello

GET /

Get the next message of the queue named by <topic>.

  • If the topic is not found. Returns: 404 Not Found
  • If the Websockets Upgrade header is found, upgrades to a websocket channel and subscribes to the topic <topic>. Each new message published to the topic <topic> are instantly published to all subscribers.

Example:

$ curl -q -o - http://localhost:8000/hello
{"id":0,"topic":{"name":"hello","ttl":60000000000,"seq":1,"created":"2018-03-25T13:18:38.732437-07:00"},"payload":"eyJtZXNzYWdlIjogImhlbGxvIn0=","created":"2018-03-25T13:18:38.732465-07:00"}

DELETE /

Deletes a queue named by <topic>.

Not implemented.

Design

Design decisions so far:

  • In memory queues (may extend this with interfaces and persistence)
  • HTTP API
  • Websockets for real-time push of events
  • Sequence ID Message tracking
  • Pull and Push model

License

msgbus is licensed under the MIT License