tystuyfzand / websub-server

Go library for the WebSub protocol, allowing you to run your own scalable WebSub Hub
ISC License
17 stars 1 forks source link
golang pubsubhubbub websub

Go WebSub Server

A Go implementation of a WebSub server. It has been tested to pass every WebSub Rocks! Hub test.

See examples/main.go for a basic example which uses boltdb and a simple publisher.

Importing:

go get meow.tf/websub

Features

Stores

Specific stores can be implemented/used to store subscriptions and call them.

If you'd like to implement your own store, the following interface can be implemented:

// Store defines an interface for stores to implement for data storage.
type Store interface {
    // All returns all subscriptions for the specified topic.
    All(topic string) ([]model.Subscription, error)

    // For returns the subscriptions for the specified callback
    For(callback string) ([]model.Subscription, error)

    // Add saves/adds a subscription to the store.
    Add(sub model.Subscription) error

    // Get retrieves a subscription given a topic and callback.
    Get(topic, callback string) (*model.Subscription, error)

    // Remove removes a subscription from the store.
    Remove(sub model.Subscription) error
}

Memory

A memory-backed store. This store is cleared when the application is restarted.

Bolt

A boltdb/bbolt backed store which persists to disk.

Workers

This hub system uses Workers to implement a system that can be infinitely scaled by adding other nodes/servers and workers which can pull off a queue.

By default, the worker pool is a basic channel + goroutine handler that goes through each request.

type Worker interface {
    Add(f PublishJob)
    Start()
    Stop()
}

When implementing workers, pay attention to the fields. ContentType is used to say what the body content type is (required by the specification), and if subscription.secret is set it MUST be used to generate an X-Hub-Signature header.

Using it with your own Publisher

If you wish to bypass the included hub.mode=publish handler, you can use the Publish function to publish your own data.

For example, if you're taking an event off some kind of queue/event subscriber:

hub.Publish("https://example.com", "application/json", []byte("{}"))