[−][src]Struct pusher::Pusher
A client to interact with Pusher's HTTP API to trigger, query application state, authenticate private- or presence-channels, and validate webhooks.
Fields
app_id: String
Your app_id from http://app.pusher.com
key: String
Your key from http://app.pusher.com
secret: String
Your secret from http://app.pusher.com
host: String
The host[:port] you wish to connect to. Defaults to api.pusherapp.com
secure: bool
If true, requests are made over HTTPS.
http_client: Client<C>
The underlying Hyper HTTP client.
Implementations
impl<C: Connect + Clone + Send + Sync + 'static> Pusher<C>
[src]
pub async fn trigger<'_, '_, '_, S: Serialize>(
&'_ self,
channel: &'_ str,
event: &'_ str,
payload: S
) -> Result<TriggeredEvents, String>
[src]
&'_ self,
channel: &'_ str,
event: &'_ str,
payload: S
) -> Result<TriggeredEvents, String>
This method allows you to trigger Pusher events. You can test this out by going on your debug console at http://app.pusher.com.
It is possible to trigger an event on one or more channels. Channel names can contain only characters which are alphanumeric, _ or -` and have to be at most 200 characters long. Event name can be at most 200 characters long too, and a payload is limited to 10kb.
This method is for triggering on only one channel, and does not allow
socket_ids to be passed in for excluding recipients. If you wish to
trigger on multiple channels, use trigger_multi
. If you wish to exclude
recipients by their socket_id, use trigger_exclusive
. For doing both,
use trigger_multi_exclusive
.
Example:
let mut hash_map = HashMap::new(); hash_map.insert("message", "hello world"); pusher.trigger("test_channel", "my_event", &hash_map);
If you call this with http://app.pusher.com open, you should receive an alert saying, 'hello world'.
This method returns a Result
. If successful, the Ok
value will be a
TriggeredEvents
instance, which, if you are connected to certain clusters,
holds the event_ids
of published events. If an error has occured,
the Error
value will contain a String
regarding what went wrong.
pub async fn trigger_exclusive<'_, '_, '_, '_, S: Serialize>(
&'_ self,
channel: &'_ str,
event: &'_ str,
payload: S,
socket_id: &'_ str
) -> Result<TriggeredEvents, String>
[src]
&'_ self,
channel: &'_ str,
event: &'_ str,
payload: S,
socket_id: &'_ str
) -> Result<TriggeredEvents, String>
This method allow you to exclude a recipient whose connection has that
socket_id
from receiving the event. You can read more here:
http://pusher.com/docs/duplicates.
Example:
pusher.trigger_exclusive("test_channel", "my_event", "hello", "123.12");
pub async fn trigger_multi<'_, '_, '_, '_, S: Serialize>(
&'_ self,
channels: &'_ [&'_ str],
event: &'_ str,
payload: S
) -> Result<TriggeredEvents, String>
[src]
&'_ self,
channels: &'_ [&'_ str],
event: &'_ str,
payload: S
) -> Result<TriggeredEvents, String>
This method allow you to trigger an event on multiple channels, with a maximum of 10.
Example:
let channels = vec!["test_channel", "test_channel2"]; pusher.trigger_multi(&channels, "my_event", "hello");
pub async fn trigger_multi_exclusive<'_, '_, '_, '_, '_, S: Serialize>(
&'_ self,
channels: &'_ [&'_ str],
event: &'_ str,
payload: S,
socket_id: &'_ str
) -> Result<TriggeredEvents, String>
[src]
&'_ self,
channels: &'_ [&'_ str],
event: &'_ str,
payload: S,
socket_id: &'_ str
) -> Result<TriggeredEvents, String>
This method allow you to trigger an event on multiple channels and exclude
a recipient with a given socket_id
.
Example:
let channels = vec!["test_channel", "test_channel2"]; pusher.trigger_multi_exclusive(&channels, "my_event", "hello", "123.12");
pub async fn channels<'_>(&'_ self) -> Result<ChannelList, String>
[src]
One can use this method to get a list of all the channels in an application from the HTTP API.
Without any supplied options, all fields for each Channel
will be None
.
If you wish to specify options for your query, see the channels_with_options
method.
An Err
will be returned for any invalid API requests.
Example:
pusher.channels(); //=> Ok(ChannelList { channels: {"presence-chatroom": Channel { occupied: None, user_count: None, subscription_count: None }, "presence-notifications": Channel { occupied: None, user_count: None, subscription_count: None }} })
pub async fn channels_with_options<'_>(
&'_ self,
params: QueryParameters
) -> Result<ChannelList, String>
[src]
&'_ self,
params: QueryParameters
) -> Result<ChannelList, String>
When adding options to your GET channels request, pass in a vector of tuples.
A tuple whose first value is "filter_by_prefix" will filter the returned channels.
To request more information, you can add a tuple beginning with "info"
to that vector.
To get number of users subscribed to a presence-channel, pass in a vector
with a ("info", "user_count")
tuple.
An Err
will be returned for any invalid API requests.
Example:
let channels_params = vec![("filter_by_prefix".to_string(), "presence-".to_string()), ("info".to_string(), "user_count".to_string())]; pusher.channels_with_options(channels_params); //=> Ok(ChannelList { channels: {"presence-chatroom": Channel { occupied: None, user_count: Some(92), subscription_count: None }, "presence-notifications": Channel { occupied: None, user_count: Some(29), subscription_count: None }} })
pub async fn channel<'_, '_>(
&'_ self,
channel_name: &'_ str
) -> Result<Channel, String>
[src]
&'_ self,
channel_name: &'_ str
) -> Result<Channel, String>
This method gets the state of a single channel.
Without any options specified, only the occupied
field of the Channel
instance
will have a value. To specify options, see the channel_with_options
method.
An Err
will be returned for any invalid API requests.
Example:
pusher.channel("presence-chatroom"); //=> Ok(Channel { occupied: Some(true), user_count: None, subscription_count: None })
pub async fn channel_with_options<'_, '_>(
&'_ self,
channel_name: &'_ str,
params: QueryParameters
) -> Result<Channel, String>
[src]
&'_ self,
channel_name: &'_ str,
params: QueryParameters
) -> Result<Channel, String>
Pass in a vector of tuples to specify options. To request information regarding
user_count
and subscription_count
, a tuple must have an "info"
value
and a value containing a comma-separated list of attributes.
An Err
will be returned for any invalid API requests.
Example:
let channel_params = vec![("info".to_string(), "user_count,subscription_count".to_string())]; pusher.channel_with_options("presence-chatroom", channel_params); //=> Ok(Channel { occupied: Some(true), user_count: Some(96), subscription_count: Some(96) })
pub async fn channel_users<'_, '_>(
&'_ self,
channel_name: &'_ str
) -> Result<ChannelUserList, String>
[src]
&'_ self,
channel_name: &'_ str
) -> Result<ChannelUserList, String>
This method retrieves the ids of users that are currently subscribed to a given presence-channel.
An Err
will be returned for any invalid API requests.
Example:
pusher.channel_users("presence-chatroom"); //=> Ok(ChannelUserList { users: [ChannelUser { id: "red" }, ChannelUser { id: "blue" }] })
pub fn authenticate_private_channel(
&self,
channel_name: &str,
socket_id: &str
) -> Result<String, &str>
[src]
&self,
channel_name: &str,
socket_id: &str
) -> Result<String, &str>
Application security is very important so Pusher provides a mechanism for authenticating a user’s access to a channel at the point of subscription.
This can be used both to restrict access to private channels, and in the case of presence channels notify subscribers of who else is also subscribed via presence events.
This library provides a mechanism for generating an authentication signature to send back to the client and authorize them.
For more information see our docs: http://pusher.com/docs/authenticating_users.
In order to authenticate a channel, pass in the body sent to your authentication endpoint upon subscription.
If an invalid body is passed in, this method will return an Err
value.
Example with hyper:
async fn pusher_auth(req: Request<Body>) -> Result<Response<Body>, Error> { let body = to_bytes(req).await.unwrap(); let params = parse(body.as_ref()).into_owned().collect::<HashMap<String, String>>(); let channel_name = params.get("channel_name").unwrap(); let socket_id = params.get("socket_id").unwrap(); let auth_signature = pusher.authenticate_private_channel(channel_name, socket_id).unwrap(); Ok(Response::new(auth_signature.into())) }
pub fn authenticate_presence_channel(
&self,
channel_name: &str,
socket_id: &str,
member: &Member
) -> Result<String, &str>
[src]
&self,
channel_name: &str,
socket_id: &str,
member: &Member
) -> Result<String, &str>
Using presence channels is similar to private channels, but in order to identify a user, clients are sent a user_id and, optionally, custom data.
In this library, one does this by passing a pusher::Member
instance. The id
field of this instance
must be a string, and any custom data will be a HashMap
wrapped in Some
.
Example with hyper
async fn pusher_auth(req: Request
let mut member_data = HashMap::new(); member_data.insert("twitter", "jamiepatel"); let member = pusher::Member{user_id: "4", user_info: Some(member_data)};
let auth_signature = pusher.authenticate_presence_channel(channel_name, socket_id, &member).unwrap(); Ok(Response::new(auth_signature.into())) }
pub fn webhook(
&self,
key: &str,
signature: &str,
body: &str
) -> Result<Webhook, &str>
[src]
&self,
key: &str,
signature: &str,
body: &str
) -> Result<Webhook, &str>
On your dashboard at http://app.pusher.com, you can set up webhooks to POST a payload to your server after certain events. Such events include channels being occupied or vacated, members being added or removed in presence-channels, or after client-originated events. For more information see https://pusher.com/docs/webhooks.
This library provides a mechanism for checking that these POST requests are indeed from Pusher, by checking the token and authentication signature in the header of the request.
Pass in the key supplied in the "X-Pusher-Key"
header, the signature supplied
in the "X-Pusher-Signature"
header, and the body of the request.
If the webhook is valid, a pusher::Webhook
instance will be returned within the Result
enum.
If not, an Err
will be returned.
Example:
pusher.webhook("supplied_key", "supplied_signature", "body")
Auto Trait Implementations
impl<C> !RefUnwindSafe for Pusher<C>
impl<C> Send for Pusher<C> where
C: Send,
C: Send,
impl<C> Sync for Pusher<C> where
C: Sync,
C: Sync,
impl<C> Unpin for Pusher<C> where
C: Unpin,
C: Unpin,
impl<C> !UnwindSafe for Pusher<C>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,