memroute

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: BSD-3-Clause Imports: 4 Imported by: 0

README

memroute

Go library that performs in-memory message routing.

Description

This is a simple layer on top of channels to provide string-based topic routing and multiple destinations.

Example

Construct a router:

import "github.com/micro-go/memroute"

r := memroute.NewRouter()

Create clients:

c1 := r.Connect()

Subscribe to topics:

c1.Subscribe("path/to/something")

Listen to the subscriptions:

for {
	select {
	...
	case m, more := <-c1.Receiver():
		if more {
			fmt.Println("received", m.Payload, "on topic", m.Topic)
		}
	}
}

And send messages, either via another client:

c2.Send("path/to/something", payload)

Or directly from the router:

r.Send("path/to/something", payload)

For greater convenience, a singleton is available if you only need a single global router:

import "github.com/micro-go/memroute"

c1 := memroute.Connect()

memroute.Send("path/to/something", payload)

Topics

Topics follow the MQTT spec: Pattern matching where / is a separator, + is a wild card match on a single level, and # is a wild card match for multiple levels. For example, a client subscribed to a/# will receive topics a/b, a/c, a/b/c etc. A client subscribed to a/+/c will receive a/b/c but not a/b/b.

Payloads

payload is an interface{} and can be anything; it's up to your application to define a data model.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Send

func Send(route string, data interface{}) error

Types

type Client

type Client interface {
	Disconnect()
	Subscribe(topic string) error
	Unsubscribe(topic string)
	Receiver() chan *Message
	Send(topic string, data interface{}) error
}

interface Client manages a single connection to the router. You can create a single client that has multiple subscriptions if you want to deal with resolving the route, or you could create a client per subscription if you want to listen to multiple channels. Note that subscribing and unsubscribing on a client is not thread safe.

func Connect

func Connect() (Client, error)

type Message

type Message struct {
	Topic   string
	Payload interface{}
}

type Router

type Router interface {
	Connect() (Client, error)
	Send(topic string, data interface{}) error
}

func NewRouter

func NewRouter() Router

func Singleton

func Singleton() Router

Jump to

Keyboard shortcuts

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