Copyright | (c) Will Sewell 2016 |
---|---|
License | MIT |
Maintainer | [email protected] |
Stability | stable |
Safe Haskell | None |
Language | Haskell2010 |
Network.Pusher
Description
Exposes the functions necessary for interacting with the Pusher Channels HTTP API, as well as functions for generating auth signatures for private and presence channels.
First create a Settings
. The easiest way of doing this is by using
defaultSettings
. From that you can use newPusher
to create a Pusher
instance and then call the functions defined in this module to make the HTTP
requests.
If any of the requests fail, the return values of the functions will result in
a Left
PusherError
when run.
An example of how you would use these functions:
let settings =defaultSettings
{pusherAddress
=Cluster
"mt1",pusherAppID
= 123,pusherToken
=Token
"wrd12344rcd234" "124df34d545v" } pusher <-newPusher
settings result <-trigger
pusher ["my-channel"] "my-event" "my-data" Nothing case result of Left e -> putStrLn $ displayException e Right resp -> print resp
There are simple working examples in the example/ directory.
See https://pusher.com/docs/channels/server_api/http-api for more detail on the HTTP requests.
Synopsis
- data Settings = Settings {}
- defaultSettings :: Settings
- data Token = Token {}
- data Address
- data Pusher
- newPusher :: MonadIO m => Settings -> m Pusher
- newPusherWithConnManager :: Manager -> Settings -> Pusher
- trigger :: MonadIO m => Pusher -> [Text] -> Text -> Text -> Maybe Text -> m (Either PusherError ())
- triggerBatch :: MonadIO m => Pusher -> [Event] -> m (Either PusherError ())
- data Event = Event {
- eventChannel :: Text
- eventName :: Text
- eventData :: Text
- eventSocketId :: Maybe Text
- channels :: MonadIO m => Pusher -> Text -> ChannelsInfoQuery -> m (Either PusherError ChannelsInfo)
- channel :: MonadIO m => Pusher -> ByteString -> ChannelInfoQuery -> m (Either PusherError FullChannelInfo)
- users :: MonadIO m => Pusher -> ByteString -> m (Either PusherError Users)
- authenticatePresence :: ToJSON a => Pusher -> Text -> Text -> a -> ByteString
- authenticatePrivate :: Pusher -> Text -> Text -> ByteString
- data PusherError
- parseWebhookPayload :: Pusher -> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload
- data WebhookEv
- = ChannelOccupiedEv { }
- | ChannelVacatedEv { }
- | MemberAddedEv { }
- | MemberRemovedEv { }
- | ClientEv { }
- data WebhookPayload = WebhookPayload {}
- data Webhooks = Webhooks {
- timeMs :: Word64
- webhookEvs :: [WebhookEv]
- parseAppKeyHdr :: ByteString -> ByteString -> Maybe ByteString
- parseAuthSignatureHdr :: ByteString -> ByteString -> Maybe ByteString
- parseWebhooksBody :: ByteString -> Maybe Webhooks
- verifyWebhooksBody :: ByteString -> ByteString -> ByteString -> Bool
- parseWebhookPayloadWith :: (ByteString -> Maybe ByteString) -> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload
Data types
Settings
All the required configuration needed to interact with the API.
Constructors
Settings | |
Fields
|
defaultSettings :: Settings Source #
A convenient way of creating an instance of Settings
. Another
benefit is it prevents breaking changes when fields are added to
Settings
, see https://www.yesodweb.com/book/settings-types.You
must set pusherAppID
and pusherToken
. Currently pusherAddress
defaults to the mt1
cluster.
Example:
defaultSettings {pusherAppID
= 123,pusherToken
=Token
{tokenKey
= "key",tokenSecret
"secret" } }
A Channels key and secret pair for a particular app.
Constructors
Token | |
Fields |
Typically you will want to connect directly to a standard Pusher Channels
Cluster
.
Constructors
Cluster ByteString | The cluster the current app resides on. Common clusters include:
|
HostPort ByteString Word16 | Used to connect to a raw address:port. |
Main Pusher type
newPusherWithConnManager :: Manager -> Settings -> Pusher Source #
Get a Pusher instance with a given connection manager. This can be useful if you want to share a connection with your application code.
HTTP Requests
Trigger events
Arguments
:: MonadIO m | |
=> Pusher | |
-> [Text] | The list of channels to trigger to. |
-> Text | Event name. |
-> Text | Event data. Often encoded JSON. |
-> Maybe Text | An optional socket ID of a connection you wish to exclude. |
-> m (Either PusherError ()) |
Trigger an event to one or more channels.
Arguments
:: MonadIO m | |
=> Pusher | |
-> [Event] | The list of events to trigger. |
-> m (Either PusherError ()) |
Trigger multiple events.
Event type for triggerBatch
Constructors
Event | |
Fields
|
Channel queries
Arguments
:: MonadIO m | |
=> Pusher | |
-> Text | A channel prefix you wish to filter on. |
-> ChannelsInfoQuery | Data you wish to query for, currently just the user count. |
-> m (Either PusherError ChannelsInfo) | The returned data. |
Query a list of channels for information.
Arguments
:: MonadIO m | |
=> Pusher | |
-> ByteString | |
-> ChannelInfoQuery | Can query user count and also subscription count (if enabled). |
-> m (Either PusherError FullChannelInfo) |
Query for information on a single channel.
users :: MonadIO m => Pusher -> ByteString -> m (Either PusherError Users) Source #
Get a list of users in a presence channel.
Authentication
authenticatePresence :: ToJSON a => Pusher -> Text -> Text -> a -> ByteString Source #
Generate an auth signature of the form "app_key:auth_sig" for a user of a presence channel.
authenticatePrivate :: Pusher -> Text -> Text -> ByteString Source #
Generate an auth signature of the form "app_key:auth_sig" for a user of a private channel.
Errors
data PusherError Source #
Constructors
Non200Response | Received non 200 response code from Pusher. |
Fields | |
InvalidResponse Text | Received unexpected data from Pusher. |
Instances
Exception PusherError Source # | |
Defined in Network.Pusher.Error Methods toException :: PusherError -> SomeException # fromException :: SomeException -> Maybe PusherError # displayException :: PusherError -> String # backtraceDesired :: PusherError -> Bool # | |
Show PusherError Source # | |
Defined in Network.Pusher.Error Methods showsPrec :: Int -> PusherError -> ShowS # show :: PusherError -> String # showList :: [PusherError] -> ShowS # |
Webhooks
parseWebhookPayload :: Pusher -> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload Source #
Parse webhooks from a list of HTTP headers and a HTTP body given their app key matches the one in our Pusher Channels credentials and the webhook is correctly encrypted by the corresponding app secret.
A WebhookEv
is one of several events Pusher may send to your server in
response to events your users may trigger.
Constructors
ChannelOccupiedEv | A channel has become occupied. There is > 1 subscriber. |
ChannelVacatedEv | A channel has become vacated. There are 0 subscribers. |
MemberAddedEv | A new user has subscribed to a presence channel. |
MemberRemovedEv | A user has unsubscribed from a presence channel. |
ClientEv | A client has sent a named client event with some json body. They have a
socket_id and a |
Fields
|
data WebhookPayload Source #
Constructors
WebhookPayload | |
Fields
|
Instances
Show WebhookPayload Source # | |
Defined in Network.Pusher.Webhook Methods showsPrec :: Int -> WebhookPayload -> ShowS # show :: WebhookPayload -> String # showList :: [WebhookPayload] -> ShowS # | |
Eq WebhookPayload Source # | |
Defined in Network.Pusher.Webhook Methods (==) :: WebhookPayload -> WebhookPayload -> Bool # (/=) :: WebhookPayload -> WebhookPayload -> Bool # |
A Webhook is received by POST request from Pusher to notify your server of
a number of WebhookEv
s. Multiple events are received under the same
timestamp if batch events is enabled.
Constructors
Webhooks | |
Fields
|
parseAppKeyHdr :: ByteString -> ByteString -> Maybe ByteString Source #
Given a HTTP Header and its associated value, parse an app key.
parseAuthSignatureHdr :: ByteString -> ByteString -> Maybe ByteString Source #
Given a HTTP Header and its associated value, parse an auth signature.
parseWebhooksBody :: ByteString -> Maybe Webhooks Source #
Given a HTTP body, parse the contained webhooks.
verifyWebhooksBody :: ByteString -> ByteString -> ByteString -> Bool Source #
Does a webhook body hash with our secret key to the given signature?
parseWebhookPayloadWith :: (ByteString -> Maybe ByteString) -> [(ByteString, ByteString)] -> ByteString -> Maybe WebhookPayload Source #
Given a list of http header key:values, a http body and a lookup function for an apps secret, parse and validate a potential webhook payload.