memq

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2022 License: MIT Imports: 1 Imported by: 0

README

memq

build Go Reference

A simple thread-safe in-memory message broker fo Go.

Quick Start

  1. Get it:

    go get github.com/sergeykonkin/memq
    
  2. Use it:

    // Import:
    import (
        "github.com/sergeykonkin/memq"
    )
    
    // Create new Broker:
    b := memq.NewBroker()
    
    // Subscribe to topic:
    b.Subscribe("my-topic", func(msg interface{}) {
        fmt.Printf("New message: %v", msg)
    })
    
    // Publish to topic:
    b.Publish("my-topic", "Hello World!")
    
    // You can also save a subscription to unsubscribe later:
    sub := b.Subscribe("my-topic", anotherHandler)
    defer sub.Unsubscribe()
    

Note that for now Broker supports only fan-out mode, so if there are 2 or more subscribers for a topic - they all will be receiving new messages.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker interface {
	// Publish publishes a message to a topic.
	// The message is delivered to all subscribers of the topic.
	Publish(topic string, msg interface{})
	// Subscribe subscribes to a topic.
	// The handler is called for each message published to the topic.
	// The returned Subscription can be used to unsubscribe from the topic.
	Subscribe(topic string, handler func(interface{})) Subscription
}

Broker handles topics, subscriptions and message delivery.

func NewBroker

func NewBroker() Broker

NewBroker creates a new Broker. The returned Broker is safe for concurrent use by multiple goroutines.

type Subscription

type Subscription interface {
	// Unsubscribe unsubscribes from a topic.
	Unsubscribe()
}

Subscription handles unsubscribing from a topic.

Jump to

Keyboard shortcuts

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