40 releases (breaking)

0.29.0 Jun 27, 2025
0.28.0 Jan 14, 2025
0.27.0 Aug 9, 2024
0.26.3 Jun 4, 2024
0.1.1 Jul 13, 2020

#2268 in Network programming

Download history 102942/week @ 2026-02-09 106398/week @ 2026-02-16 115621/week @ 2026-02-23 133255/week @ 2026-03-02 147012/week @ 2026-03-09 123904/week @ 2026-03-16 122020/week @ 2026-03-23 144714/week @ 2026-03-30 140482/week @ 2026-04-06 155458/week @ 2026-04-13 144985/week @ 2026-04-20 144450/week @ 2026-04-27 136166/week @ 2026-05-04 129217/week @ 2026-05-11 155474/week @ 2026-05-18 143797/week @ 2026-05-25

578,602 downloads per month
Used in 501 crates (9 directly)

MIT license

1MB
16K SLoC

Generic request/response protocols.

General Usage

The Behaviour struct is a NetworkBehaviour that implements a generic request/response protocol or protocol family, whereby each request is sent over a new substream on a connection. Behaviour is generic over the actual messages being sent, which are defined in terms of a Codec. Creating a request/response protocol thus amounts to providing an implementation of this trait which can then be given to Behaviour::with_codec. Further configuration options are available via the Config.

Requests are sent using Behaviour::send_request and the responses received as Message::Response via Event::Message.

Responses are sent using Behaviour::send_response upon receiving a Message::Request via Event::Message.

Predefined codecs

In case your message types implement serde::Serialize and serde::Deserialize, you can use two predefined behaviours:

Protocol Families

A single Behaviour instance can be used with an entire protocol family that share the same request and response types. For that purpose, Codec::Protocol is typically instantiated with a sum type.

Limited Protocol Support

It is possible to only support inbound or outbound requests for a particular protocol. This is achieved by instantiating Behaviour with protocols using ProtocolSupport::Inbound or ProtocolSupport::Outbound. Any subset of protocols of a protocol family can be configured in this way. Such protocols will not be advertised during inbound respectively outbound protocol negotiation on the substreams.

Dependencies

~12–18MB
~240K SLoC