Documentation
¶
Index ¶
- type Messenger
- func (m *Messenger) Broadcast(ctx context.Context, out serde.Message) <-chan error
- func (m *Messenger) Close() error
- func (m *Messenger) ClosedStream(_ network.Network, _ network.Stream)
- func (m *Messenger) Connected(_ network.Network, c network.Conn)
- func (m *Messenger) Disconnected(_ network.Network, _ network.Conn)
- func (m *Messenger) Events() <-chan PeerEvent
- func (m *Messenger) Host() host.Host
- func (m *Messenger) Listen(_ network.Network, _ ma.Multiaddr)
- func (m *Messenger) ListenClose(_ network.Network, _ ma.Multiaddr)
- func (m *Messenger) OpenedStream(_ network.Network, _ network.Stream)
- 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) <-chan error
- type Option
- type PeerEvent
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 sends the given message 'out' to each connected peer speaking the same protocol. 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 stop the Messenger and unregisters further protocol handling on the Host.
func (*Messenger) ClosedStream ¶
func (*Messenger) Disconnected ¶
func (*Messenger) Events ¶
Events notifies about connection state changes of peers. If messenger is started over Host with existing connections, for every existing peer there will be an event.
func (*Messenger) OpenedStream ¶
func (*Messenger) Receive ¶
Receive awaits for incoming messages from peers. It receives messages sent through both Send and Broadcast. Ti errors only if the given context 'ctx' is closed or when Messenger itself is close.
func (*Messenger) Send ¶
Send sends the given message 'out' to the 'peer' to. The returned channel is always closed once the message is sent successfully or with an error. 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.