Struct hyper_sse::Server [−][src]
pub struct Server<C> { /* fields omitted */ }
Push server implementing Server-Sent Events (SSE).
SSE allow pushing events to browsers over HTTP without polling. This library uses async hyper to support many concurrent push connections and is compatible with the Rocket framework. It supports multiple parallel channels and client authentication.
The generic parameter C
specifies the type used to distinguish
the different channels and can be chosen arbitrarily.
Because the Server implements Sync
, it can e.g. be stored
in a static variable using lazy_static
.
Methods
impl<C: DeserializeOwned + Eq + Hash + FromStr + Send + Serialize> Server<C>
[src]
impl<C: DeserializeOwned + Eq + Hash + FromStr + Send + Serialize> Server<C>
pub fn new() -> Server<C>
[src]
pub fn new() -> Server<C>
Create a new SSE push-server.
pub fn push<S: Serialize>(
&self,
channel: C,
event: &str,
message: &S
) -> Result<(), Error>
[src]
pub fn push<S: Serialize>(
&self,
channel: C,
event: &str,
message: &S
) -> Result<(), Error>
Push a message for the event to all clients registered on the channel.
The message is first serialized and then send to all registered clients on the given channel, if any.
Returns an error if the serialization fails.
pub fn create_stream(&self, request: &Request<Body>) -> Response<Body>
[src]
pub fn create_stream(&self, request: &Request<Body>) -> Response<Body>
Initiate a new SSE stream for the given request.
The request must include a valid authorization token. The channel is parsed from the last segment of the uri path. If the request cannot be parsed correctly or the auth token is expired, an appropriate http error response is returned.
pub fn generate_auth_token(&self, channel: Option<C>) -> Result<String, Error>
[src]
pub fn generate_auth_token(&self, channel: Option<C>) -> Result<String, Error>
Create an opaque authorization token that will be checked
in create_stream
before establishing the SSE stream.
A new token can be send to the client on every request, as creating and checking the tokens is cheap. The token is valid for 24 hours after it has been generated and can only be used on the specified channel if specified. The token must be passed as the query url segment to the sse endpoint.
Returns an error if the channel serialization fails.
pub fn send_heartbeats(&self)
[src]
pub fn send_heartbeats(&self)
Send hearbeat to all clients on all channels.
This should be called regularly (e.g. every minute) to detect a disconnect of the underlying TCP connection.
pub fn remove_stale_clients(&self)
[src]
pub fn remove_stale_clients(&self)
Remove disconnected clients.
This removes all clients from all channels that have closed the connection or are not responding to the heartbeats, which caused a TCP timeout.
This function should be called regularly (e.g. together with
send_heartbeats
) to keep the memory usage low.
pub fn spawn(&'static self, listen: SocketAddr) -> JoinHandle<()>
[src]
pub fn spawn(&'static self, listen: SocketAddr) -> JoinHandle<()>
Run a push SSE server on the given address.
Convenience function for starting a push server on a new thread.
Maintenance is done automatically, so you don't have to call
send_heartbeats
or remove_stale_clients
.
This function will panic in the current thread if it cannot listen on the specified address.