#tokio #websocket #codec #serverproto


An implementation of the RFC6455 websocket protocol as a tokio Codec and a tokio-proto pipeline ServerProto

15 releases (6 breaking)

Uses old Rust 2015

0.7.1 Mar 21, 2017
0.7.0 Mar 19, 2017
0.6.0 Mar 16, 2017
0.5.3 Mar 13, 2017
0.1.4 Feb 23, 2017

#79 in #codec

Download history 6/week @ 2023-06-05 8/week @ 2023-06-12 33/week @ 2023-06-19 13/week @ 2023-06-26 32/week @ 2023-07-03 22/week @ 2023-07-10 13/week @ 2023-07-17 48/week @ 2023-07-24 8/week @ 2023-07-31 9/week @ 2023-08-07 26/week @ 2023-08-14 7/week @ 2023-08-21 22/week @ 2023-08-28 15/week @ 2023-09-04 23/week @ 2023-09-11 23/week @ 2023-09-18

83 downloads per month
Used in 5 crates




An implementation of the RFC6455 websocket protocol as a tokio-core Codec and a tokio-proto pipeline ServerProto

This can be used to serve up a tokio-service Service in a tokio-proto TcpServer (See twists for an example).

Note: Even though this is implemented as a pipeline protocol, fragmented websocket messages are supported, so streaming from a websocket client is supported.


Include the following in your Cargo.toml dependencies section

twist = "0.1.3"


Per Message extensions are now supported by twist. A per-message extension is applied when a text or binary frame is complete, or a set of fragmented text or binary frames have been fully assembled.


twist exposes three traits relevant for implementing an extension:

  1. FromHeader - Extensions are configured based on the contents of the Sec-WebSocket-Extensions request header. Implement the init function to configure your extension based off of this header. You should return an io::Error if you are unable to support the given header parameters. If the parameters aren't relevant to your extension, you should ensure that your enabled function returns false.

     fn init(&mut self, request: &str) -> Result<(), io::Error> {
         if request.contains("permessage-deflate") {
             self.enabled = true;
             let pmds: Vec<&str> =
                 request.split(',').take_while(|s| s.starts_with("permessage-deflate")).collect();
             for pmd in pmds {
                 // Check the config here.
                 // return io::Error if it is invalid.
  2. IntoResponse - Implement the response function to return the Sec-WebSocket-Extensions response header. If you are disabled, return None.

  3. PerMessage - This is the meat of the extension. There are three methods to implement.

    a. reserve_rsv - If your extension requires the use of one of the frame's rsv bits, you can attempt to reserve it. The value you receive represents the bits that have already been reserved previously in the extension chain. Check if your bit (or bits) are free, and bitwise or your bits with the given, and return that value. Below is an example from twist-deflate. The deflate extension requires the rsv1 bit, so RSV1 is 4 (0b100).

     if current_rsv & RSV1 == 0 {
       Ok(current_rsv | RSV1)
     } else {
       Err(other("rsv1 bit already reserved"))

    b. decode - Implement this to apply decoding to a message frame (for example decompressing the application data).

    c. encode - Implement this to apply encoding to a message frame before final encoding by twist (for example compressing the application data).

See twist-deflate for a more complete example.



Example Usage

See the twists project for an example implementation of an echo server using twist.


~167K SLoC