socket-flow
Straightforward async Websockets library for Rust! With a lot of examples available!
Introduction
This library is supposed to offer a simple implementation for websockets, so end-user could use this to wrap a websockets server/client into their application, offering a smooth way of setting it up into his code.
It's an async library based on tokio runtime, which uses a tokio TcpStream behind the scenes, using that as the starting point to implement the standards of WebSocket Protocol RFC, performing handshakes, reading frames, parsing masks, handling opcodes and internal payload.
Can be used as a client or server,
returning a WSConnection
, which implements the Stream
trait,
so you can continuously consume incoming messages, or send messages.
The motivation behind this, was to offer a simple way of having a WebSockets connection over your application, using as a
reference wide established libraries, like tungstenite-rs
and tokio-tungstenite
Features
Most of all WebSockets RFC features are implemented, like:
- Handshake process, key parsing and generation
- OpCodes handling, like
Text
,Binary
,Ping
,Pong
andContinue
- Multiple subscriptions
- Scalability
- Error handling
- It passes the autobahn-test-suite
- TLS Support
- Extensions (Compression and Decompression by permessage-deflate)
Usage
Add this in your Cargo.toml
:
[]
= "*"
Examples of usage
This repo has different examples and flexible ways of using its dependencies to design the code as end-user needs.
We have the option of configuring all from scratch, creating the TcpListener, and managing the websockets connections, and we also have a plug-and-play option, which you can generate a Websockets server, with fewer lines of code.
Plug and play server
This is a very practical example, because you can have a server with just calling start_server
function, which returns
an EventStream
, for consuming server events, like new connections, messages, errors and disconnections.
You can also find in: Example
The start_server
function also accepts a rustls::ServerConfig
for enabling TLS in your server.
use StreamExt;
use *;
use ;
use start_server;
use WSWriter;
use HashMap;
async
For running this example, you can clone the repo and execute:
cargo run --color=always --package socket-flow --example simple_server
Echo server
Here is an echo-server example that you can also find in: Example
use StreamExt;
use *;
use accept_async;
use SocketFlowStream;
use SocketAddr;
use ;
async
async
For running this example, you can clone the repo and execute:
cargo run --color=always --package socket-flow --example echo_server
This example, creates a TcpListener, binding it to a port, accepting connections, handling each of these connections
inside a tokio task, for handling clients concurrently. The handle_connection function, make sure the handshake process
is performed, returning a WSConnection
, which implements Stream
trait, where you can consume incoming data for this client,
and perform write operations into the socket.
It includes error handling through Result
.
Simple client
Here is an example of how to run a client, that will perform some operations and disconnect gracefully:
use StreamExt;
use *;
use Alphanumeric;
use ;
use connect_async;
use select;
use ;
async
async
Since you need a server for testing the client, you can execute our echo-server example, and on another tab execute the client example:
cargo run --color=always --package socket-flow --example client
In this example, the client will try to connect to ws://127.0.0.1:9002
,
if the connection is established, it will start sending random strings every 5 seconds into the socket.
After sending three strings, it will close the connection gracefully and end its execution.
You can check more examples over Examples.
Testing
Socket-flow passes the Autobahn Test Suite for WebSockets. Also, it has some internal tests, for ensuring reliability.
TLS/SSL
By default, this library only accepts tokio-rustls, as an adapter library for adding TLS in your client/server implementation with socket-flow.
For checking how to set up TLS in server/client, and finding some examples, go to: TLS Examples.
Config and Compression
For setting some parameters of your websockets connection, and enabling compression and decompression, you can check how to set up that over Config and Extensions.