Module message

Source
Expand description

MessageHandler trait and implementations, used to handle the message events. It has a flexible axum-like API, you can put any arguments as long as it implements the FromMessageParts trait or the FromMessage trait for the last argument.

All the types that implement FromMessageParts also implement FromMessage.

You can also implement the FromMessageParts and FromMessage traits for your own types. See the extract module doc for more details on available extractors.

Handlers can be optionally async.

§Example with sync closures

let (svc, io) = SocketIo::new_svc();
io.ns("/", |s: SocketRef| {
    // We listen for the "event" event and we deserialize the data to a `String`.
    // In case of deserialization failure the handler is not called.
    // A TryData extractor can be used instead to get serde errors.
    s.on("event", |s: SocketRef, Data::<String>(data)| {
       println!("Socket received event with data: {}", data);
    });

    // We listen for the event_with_ack event.
    // Here the data is not deserialized and dropped as it is not in the arguments list
    s.on("event_with_ack", |s: SocketRef, ack: AckSender| {
      ack.send("ack data").ok();
    });

    // `Bin` extractor must be the last argument because it consumes the rest of the packet
    s.on("binary_event", |s: SocketRef, TryData::<String>(data)| {
      println!("Socket received event with data: {:?}", data);
    })
});

§Example with async closures

let (svc, io) = SocketIo::new_svc();
io.ns("/", |s: SocketRef| {
    s.on("event", move |s: SocketRef, Data::<String>(data)| async move {
       tokio::time::sleep(std::time::Duration::from_secs(1)).await;
       println!("Socket received event with data: {}", data);
    });
    // `Bin` extractor must be the last argument because it consumes the rest of the packet
    s.on("/binary_event", move |s: SocketRef, TryData::<String>(data)| async move {
      println!("Socket received event with data: {:?}", data);
    })
});

§Example with an async non-anonymous handler

// async named event handler
async fn on_event(s: SocketRef, Data(data): Data<serde_json::Value>, ack: AckSender) {
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    ack.send("Here is my acknowledgment!").ok();
}
let (svc, io) = SocketIo::new_svc();
io.ns("/", |s: SocketRef| {
    s.on("event", on_event);
    // It is also possible to reuse handlers, like this:
    s.on("event_2", on_event);
});

Traits§

FromMessage
A trait used to extract and consume arguments from the message event. The Result associated type is used to return an error if the extraction fails, in this case the handler is not called.
FromMessageParts
A trait used to extract arguments from the message event. The Result associated type is used to return an error if the extraction fails, in this case the handler is not called.
MessageHandler
Define a handler for the connect event. It is implemented for closures with up to 16 arguments. They must implement the FromMessageParts trait or the FromMessage trait for the last one.