redisbus

package module
v0.0.0-...-c00e4a4 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2018 License: MIT Imports: 6 Imported by: 0

README

Go Report Card GoDoc

redisbus

A pubsub bus built on top of Redis. This library will multiplex Redis subscriptions to a larger number of local subscriptions.

To get started:

$ go get github.com/charlesworth/redisbus

Interface

func New(redisURL string, dialTimeout time.Duration) (Bus, error)
func NewWithLogger(redisURL string, dialTimeout time.Duration, logger *log.Logger) (Bus, error)

type Bus interface {
    Publish(channel string, data []byte) error
    Subscribe(channel string) (Subscription, error)
    ExitChan() <-chan error
    Close()
}

type Subscription interface {
    DataChan() <-chan []byte
    ExitChan() <-chan struct{}
    Close()
}

Example

package main

import (
    "fmt"
    "github.com/charlesworth/redisbus"
    "time"
)

func main() {
    bus, _ := redisbus.New(":6379", time.Second)

    channelName := "chat"
    subscriber, _ := bus.Subscribe(channelName)

    message := []byte("Hello Subscribers")
    bus.Publish(channelName, message)

    fmt.Printf("%s\n", <-subscriber.DataChan())
}

Testing

Requires a running Redis instance on port :6379

$ go test

Documentation

Index

Constants

View Source
const (
	SubscriptionReadError = "SubscriptionReadError"
	UnsubscribeError      = "UnsubscribeError"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	Publish(channel string, data []byte) error
	Subscribe(channel string) (Subscription, error)
	ExitChan() <-chan error
	Close()
}

Bus contains the connection to Redis and supplies the Publish and Subscribe methods to access the PubSub features. ExitChan will be closed when the subscription has ended. Call Close to end the subscription manually.

func New

func New(redisURL string, dialTimeout time.Duration) (Bus, error)

New initialises and starts a redisbus Bus instance

func NewWithLogger

func NewWithLogger(redisURL string, dialTimeout time.Duration, logger *log.Logger) (Bus, error)

NewWithLogger initialises and starts a redisbus Bus instance with a logger

type Subscription

type Subscription interface {
	DataChan() <-chan []byte
	ExitChan() <-chan struct{}
	Close()
}

Subscription is an object that provides subscription data over the DataChan. ExitChan will be closed when the subscription has ended. Call Close to end the subscription manually.

Jump to

Keyboard shortcuts

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