Documentation
¶
Index ¶
- type Message
- type MessageBase
- type Messenger
- func (m *Messenger[M]) Broadcast(ctx context.Context, out M) (peer.IDSlice, error)
- func (m *Messenger[M]) Close() error
- func (m *Messenger[M]) Host() host.Host
- func (m *Messenger[M]) Peers() peer.IDSlice
- func (m *Messenger[M]) Receive(ctx context.Context) (msg M, err error)
- func (m *Messenger[M]) Send(ctx context.Context, out M) error
- type NewMessageFn
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Message ¶ added in v0.2.1
type Message interface {
serde.Message
// From points to peer the Message was received from.
From() peer.ID
// To points to Messenger the peer to send the Message to.
To() peer.ID
}
Message is a contract that any user defined message has to satisfy to be sent via Messenger.
type MessageBase ¶ added in v0.2.1
MessageBase is a helper to struct that users may embed into their custom Message implementations. It's sole purpose is to minimize boilerplate code.
func (*MessageBase) From ¶ added in v0.2.1
func (mb *MessageBase) From() peer.ID
func (*MessageBase) To ¶ added in v0.2.1
func (mb *MessageBase) To() peer.ID
type Messenger ¶
type Messenger[M Message] struct { // contains filtered or unexported fields }
Messenger provides a simple API to send messages to multiple peers.
func New ¶
New instantiates a Messenger. WithProtocols option is mandatory for at least one protocol. WithMessageType overrides default serde.PlainMessage.
func (*Messenger[M]) Broadcast ¶
Broadcast optimistically sends the given message 'out' to each connected peer speaking *the same* protocol. It returns a slice of peers to whom the message was sent and errors in case the given ctx was closed or in case when Messenger is closed. WARNING: It should be used deliberately. Avoid use cases requiring message propagation to a whole protocol network, not to flood the network with message duplicates. For such cases use libp2p.PubSub instead.
func (*Messenger[M]) Close ¶
Close stops the Messenger and unregisters further protocol handling on the Host.
func (*Messenger[M]) Peers ¶ added in v0.2.0
Peers returns a list of connected/immediate peers speaking the protocol registered on the Messenger.
func (*Messenger[M]) Receive ¶
Receive awaits for incoming messages from peers. It receives messages sent through both Send and Broadcast. It errors only if the given context 'ctx' is closed or when Messenger is closed.
func (*Messenger[M]) Send ¶
Send optimistically sends the given message 'out' to the peer 'to'. It errors in case the given ctx was closed or in case when Messenger is closed. All messages are sent in a per peer queue, so the ordering of sent messages is guaranteed. In case the Messenger is given with a RoutedHost, It tries to connect to the peer, if not connected.
type NewMessageFn ¶ added in v0.2.1
NewMessageFn is a type-parameterized constructor func for Message. It is required by Messenger, s.t. it can instantiate user-defined messages coming from the network.