Skip to content

coze-cloud/messenger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

messenger

📬 A minimalistic library for abstracting asynchronus messaging (inspired by Vice)

Installation

Adding messenger to your Go module is as easy as calling this command in your project

go get github.com/coze-cloud/messenger

Usage

Being a minimalistic library, messenger only provides the basics. In essence the basics are transport layer abstractions and a simple (but opinonated) message implementation. The later however is completly independend from the transport layer. Thus you can easily build your own way of handling messages!

Currently supported transports are:

  • In-Memory / Local
  • RabitMQ / AMQP
  • Redis Streams

Setting up a messenger

ctx, cancel := context.WithCancel(context.Background)
defer cancel()

msgr := local.NewLocalMessenger(ctx)

Receiving data

Receiving and sending data utilizes Go channels, which work just as you expect them to!

The abstraction and thus the use of exchange and consumer in messenger is heavily inspired by the way messages are processed by RabbitMQ using the Publish/Subscribe transport.

data, ok := <- msgr.Receive("exchange", "consumer")
if !ok {
    // Channel is closed, so no further data will arrive
    return
}
fmt.Println(string(data))

Sending data

msgr.Send("exchange") <- []byte("Hello world!")

IMPORTANT: Do not close send channels after no furhter transmission will take place, as they are shared instances acros the life of your program.


Copyright © 2022 - The cozy team & contributors