modulepackage
0.2.0
Repository: https://github.com/go-broadcast/broadcast.git
Documentation: pkg.go.dev
# README
Broadcast
A small utility that allows you to distribute messages to groups of long-lived connections. The implementation is connection ignorant so it could be used with different communication mechanisms like web sockets and gRPC server streams.
Installation
go get github.com/go-broadcast/broadcast
Basic usage
import (
"log"
"time"
"github.com/go-broadcast/broadcast"
)
func main() {
broadcaster, cancel, err := broadcast.New()
if err != nil {
log.Fatal(err)
}
subscription := broadcaster.Subscribe(func(data interface{}) {
log.Printf("Received message: %v", data)
})
broadcaster.JoinRoom(subscription, "chat-room")
broadcaster.ToRoom("Hello, chat!", "chat-room")
<-time.After(time.Second * 10)
broadcaster.ToRoom("Bye, chat!", "chat-room")
broadcaster.Unsubscribe(subscription)
cancel()
<-broadcaster.Done()
}
More examples
# Functions
New creates a new Broadcaster.
WithDefaultRoomName sets the name of the default room.
WithDispatcher sets a Dispatcher implementation.
WithPoolSize limits the amount of go routines that can be used when sending messages to a large number of subscribers.
WithPoolTimeout sets the duration a go routine responsible for sending messages to subscribers will linger after it is done with sending mesasges.
# Structs
Subscription represents a receiver of messages.
# Interfaces
Broadcaster defines all broadcast operations.
Dispatcher allows messages to be dispatched to external services.
# Type aliases
CancelFunc represents a function used to cancel all go routines used by the Broadcaster.
Option is used to change broadcaster settings.