package module
Version: v1.1.3 Latest Latest

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

Go to latest
Published: Nov 2, 2019 License: MIT Imports: 1 Imported by: 7



Simple broadcasting mechanism using go channels

Build Status codecov GoDoc Go Report Card Donate

What is this

This is a simple broadcasting mechanism where gouroutines can subscribe and unsubscribe to recieve messages from the Messenger instance.

Channels can be buffered to reduce blocking and messages can be dropped if channel's buffer is full. These are configurable options.


Because i needed this for some silly stuff and ofcourse, why not!



Package messenger provides a simple broadcasting mechanism




This section is empty.


This section is empty.


This section is empty.


type Messenger

type Messenger struct {
	// contains filtered or unexported fields

Messenger instance. Must be invoked with New().

func New

func New(buffer uint, drop bool) *Messenger

New creates a new Messenger instance.

Buffer sets the buffer size to client channels, set to 0 if you want unbuffered behaviour.

Drop makes broadcasts to drop if a client's buffer is full. Ignored with 0 buffer size.

package main

import (


func main() {
	log.SetPrefix("time ")
	m := messenger.New(0, false)
	wg := &sync.WaitGroup{}
	for i := 1; i <= 5; i++ {
		go func(i int, m *messenger.Messenger) {
			defer wg.Done()
			time.Sleep(time.Millisecond * time.Duration(i*20))
			client, err := m.Sub()
			if err != nil {
				log.Printf("Client %d: %v\n", i, err)
			log.Printf("Client %d subscribed\n", i)
			timeout := time.After(time.Millisecond * time.Duration(i*100))
			for {
				select {
				case msg := <-client:
					log.Printf("Client %d got message: %s\n", i, msg)
				case <-timeout:
					log.Printf("Client %d unsubscribed\n", i)
		}(i, m)
	for i := 0; i < 10; i++ {
		time.Sleep(time.Millisecond * 50)
		m.Broadcast(fmt.Sprintf("nr.%d", i))

func (*Messenger) Broadcast

func (m *Messenger) Broadcast(msg interface{})

Broadcast broadcasts a message to all current clients. If a client is not listening and drop is not set this will block.

func (*Messenger) Kill

func (m *Messenger) Kill()

Kill closes and removes all clients and stops the monitor() goroutine of Messenger, making the Messenger instance unusable. Kill should only be called when all clients have exited.

func (*Messenger) Len added in v1.1.1

func (m *Messenger) Len() int

Len gets the subscriber count

func (*Messenger) Reset

func (m *Messenger) Reset()

Reset closes and removes all clients.

func (*Messenger) Sub

func (m *Messenger) Sub() (client chan interface{}, err error)

Sub returns a new client. Clients can block Broadcast() unless drop is set. Clients should check whether the channel is closed or not. Returns an error if its Messenger instance is Killed. You can ignore the err if you never intend to use Kill()

func (*Messenger) Unsub

func (m *Messenger) Unsub(client chan interface{})

Unsub unsubscribes a client and closes its channel.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL