pubsub

package module
v0.0.0-...-0dda1d1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: MIT Imports: 1 Imported by: 0

README

MatteoPaier/pubsub

GoDoc

A lightweight go package for Publish/Subscribe communication via channels.

Install

Install pubsub with:

go get github.com/MatteoPaier/pubsub

License

Use of this module is governed by a MIT license that can be found in the LICENSE file.

Documentation

Overview

Package pubsub provides extremely lightweight primitives for Publisher/Subscribe communication between goroutines. It allows creation of Pub/Sub groups and hierarchical topics.

Example
var ps PubSub

go func() {
	for {
		ps.Publish("topic1", "test")
		time.Sleep(1 * time.Second)
	}
}()

go func() {
	for {
		ps.Publish("topic2", "example")
		time.Sleep(2 * time.Second)
	}
}()

ch := ps.Subscribe("topic1.subtopic")
for i := 0; i < 10; i++ {
	msg := <-ch
	fmt.Println("topic1.subtopic", msg)
}
ps.Unsubscribe(ch)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PubSub

type PubSub struct {
	// contains filtered or unexported fields
}

PubSub is a group of Publishers and Subscribers.

func (*PubSub) Publish

func (ps *PubSub) Publish(topic string, message interface{})

Publish allows to write on a specific topic (and its descendants).

func (*PubSub) Subscribe

func (ps *PubSub) Subscribe(topic string) chan interface{}

Subscribe allows for subscription to a specific topic. It returns a channel that can be used to get the messages from the topic specified. A topic can be a subtopic of another. E.g.

ps.Subscribe("topic1.subtopic")

receives also the messages from topic1.

func (*PubSub) Unsubscribe

func (ps *PubSub) Unsubscribe(channel chan interface{})

Unsubscribe allows for the removal of a subscriber's channel. It requires a channel created with Subscribe.

Jump to

Keyboard shortcuts

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