eventbus

package
v0.0.0-...-71c6276 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

README

eventbus

Eventbus provides a simple event bus (or pub / sub mechanism) which can be extended by an adapter, such as WebSocket handler.

Simple websocket example

bus := eventbus.Bus{}
Listen for message
func listenSupport(w http.ResponseWriter, r *http.Request) {
    adapter, err := eventbus.NewWsAdapter(w, r)
    if err != nil {
      // ...
    }
    bus.Register(adapter).
      ListenTopics("outgoing1", "outgoing2"). // topics which will be sent out via the websocket
      PublishTopics("incoming1", "incoming2") // topics to publish to when message originating from the remote websocket
    
    // you can also attach listeners directly to the websocket adapter
    adapter.OnIncoming(func(message interface{}) {
      fmt.Printf("--- webservice received a message: %s", message)
    })
    adapter.OnOutgoing(func(message interface{}) {
      fmt.Printf("--- webservice sent a message: %s", message)
    })
    adapter.OnClose(func(reason string) {
      fmt.Printf("--- webservice has been closed: %s", reason)
    })
}
Dispatch message

To dispatch message into the bus (which is then passed to the websocket)

bus.Publish("outgoing1", "message I want to send out using websocket")
Subscribe to topic

To subscribe to the topic (which is populated by the websocket), do

bus.Subscribe("incoming1", func(message interface{}){
	// Something on the other end of the websocket has sent the message
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogDebug func(message string, a ...interface{}) = func(message string, a ...interface{}) {}

LogDebug represents log function for the websocket

View Source
var LogError func(message string, a ...interface{}) = func(message string, a ...interface{}) {}

LogError represents log function

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	OnIncoming(...func(message interface{})) // listening to incoming messages from the outer world
	OnOutgoing(...func(message interface{})) // listening on outgoing messages
	OnClose(...func(reason string))          // listening to close event
	SendOut(message interface{}) error       // sending outgoing messages
	Close(reason string)                     // closing remote connection
}

Adapter represents interface used by the

func NewWsAdapter

func NewWsAdapter(w http.ResponseWriter, r *http.Request) (result Adapter, resultErr error)

NewWsAdapter is a constructor of ws adapter

type Bus

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

Bus represents event bus

func (*Bus) Introspect

func (b *Bus) Introspect()

Introspect prints all handlers

func (*Bus) Publish

func (b *Bus) Publish(topicID string, message interface{})

Publish publishes message to the topic

func (*Bus) Register

func (b *Bus) Register(handler Adapter) (result *BusRegistration)

Register register handler against set of topics

func (*Bus) Subscribe

func (b *Bus) Subscribe(topicID string, listener func(message interface{})) (cancel func())

Subscribe subscribes for updates on the topic

type BusRegistration

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

BusRegistration is returned by the Bus and exposes additional setters

func (*BusRegistration) ListenTopics

func (r *BusRegistration) ListenTopics(topicIDs ...string) *BusRegistration

ListenTopics sets handler to listen on specific topics

func (*BusRegistration) PublishTopics

func (r *BusRegistration) PublishTopics(topicIDs ...string) *BusRegistration

PublishTopics sets handler to publish to specific topics

Jump to

Keyboard shortcuts

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