Documentation ¶
Index ¶
- type Messenger
- func (m *Messenger) Broadcast(ctx context.Context, out serde.Message) (peer.IDSlice, error)
- func (m *Messenger) Close() error
- func (m *Messenger) Host() host.Host
- func (m *Messenger) Peers() []peer.ID
- func (m *Messenger) Receive(ctx context.Context) (serde.Message, peer.ID, error)
- func (m *Messenger) Send(ctx context.Context, out serde.Message, to peer.ID) error
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Messenger ¶
type Messenger 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) Broadcast ¶
Broadcast optimistically sends the given message 'out' to each connected peer speaking *the same* protocol. It returns a slice od 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) Close ¶
Close stops the Messenger and unregisters further protocol handling on the Host.
func (*Messenger) Peers ¶ added in v0.2.0
Peers returns a list of connected/immediate peers speaking the protocol registered on the Messenger.
func (*Messenger) 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) 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 Option ¶
type Option func(messenger *Messenger)
Option defines an functional configurability for Messenger.
func WithMessageType ¶
WithMessageType sets a custom message type for receiving messages. Otherwise Messenger would not know what type to return in Receive.
func WithProtocols ¶
WithProtocols sets custom protocols for messenger to speak with At least one protocol is required.