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]

Create a new SSE push-server.

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.

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.

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.

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.

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.

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.

Auto Trait Implementations

impl<C> Send for Server<C> where
    C: Send

impl<C> Sync for Server<C> where
    C: Send