pub struct ChannelEvent { /* private fields */ }
Expand description

The messages passed between server and connected clients.

ChannelEvents contain a topic, event, payload, and if sent from a client, a unique reference identifier that can be used to respond to this event.

Most interfaces in this crate take an Into<ChannelEvent> instead of a ChannelEvent directly, so that you can either implement Into for relevant types, or use these tuple From implementations:

use trillium_channels::ChannelEvent;
use serde_json::{json, Value, to_string};
let event: ChannelEvent = ("topic", "event").into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(event.payload(), &json!({}));

let event: ChannelEvent = ("topic", "event", &json!({"some": "payload"})).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"some":"payload"}"#);

#[derive(serde::Serialize)]
struct SomePayload { payload: &'static str };
let event: ChannelEvent = ("topic", "event", &SomePayload { payload: "anything" }).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"payload":"anything"}"#);

Implementations

Construct a new ChannelEvent with the same reference as this ChannelEvent. Note that this is the only way of setting the reference on an event.

The event argument can be either a String or, more commonly, a &'static str.

The topic will always be the same as the source ChannelEvent’s topic.

Returns this ChannelEvent’s topic

Returns this ChannelEvent’s event

Returns this ChannelEvent’s payload as a Value

Returns the reference field (“ref” in json) for this ChannelEvent, if one was provided by the client

Constructs a new ChannelEvent from topic, event, and a serializable payload. Use &() if no payload is needed.

Note that the reference cannot be set this way. To set a reference, use ChannelEvent::build_reply

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Converts to this type from the input type.
Converts to this type from the input type.
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.