Crate trillium_websockets[][src]

Expand description

A websocket trillium handler

There are three primary ways to use this crate

With an async function that receives a WebSocketConn

This is the simplest way to use trillium websockets, but does not provide any of the affordances that implementing the WebSocketHandler trait does. It is best for very simple websockets or for usages that require moving the WebSocketConn elsewhere in an application. The WebSocketConn is fully owned at this point, and will disconnect when dropped, not when the async function passed to websocket completes.

use futures_lite::stream::StreamExt;
use trillium_websockets::{Message, WebSocketConn, websocket};

let handler = websocket(|mut conn: WebSocketConn| async move {
    while let Some(Ok(Message::Text(input))) = {
        conn.send_string(format!("received your message: {}", &input)).await;

Implementing WebSocketHandler

WebSocketHandler provides support for sending outbound messages as a stream, and simplifies common patterns like executing async code on received messages.

Using JsonWebSocketHandler

JsonWebSocketHandler provides a thin serialization and deserialization layer on top of WebSocketHandler for this common use case. See the JsonWebSocketHandler documentation for example usage. In order to use this trait, the json cargo feature must be enabled.


pub use async_tungstenite;
pub use async_tungstenite::tungstenite;


A wrapper type for JsonWebSocketHandlers

The trillium handler. See crate-level docs for example usage.

A struct that represents an specific websocket connection.


Possible WebSocket errors.

An enum representing the various forms of a WebSocket message.


Implement this trait to use websockets with a json handler

This is the trait that defines a handler for trillium websockets.


builds a new trillium handler from the provided JsonWebSocketHandler. Alias for WebSocket::new_json

Builds a new trillium handler from the provided WebSocketHandler. Alias for WebSocket::new

Type Definitions

a Result type for websocket messages