pusher-http-haskell-2.1.0.17: Haskell client library for the Pusher Channels HTTP API
Copyright(c) Will Sewell 2016
LicenseMIT
Maintainerme@willsewell.com
Stabilitystable
Safe HaskellSafe-Inferred
LanguageHaskell2010

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 types

Settings

data Settings Source #

All the required configuration needed to interact with the API.

Instances

Instances details
FromJSON Settings Source # 
Instance details

Defined in Network.Pusher.Data

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" }
  }

data Token Source #

A Channels key and secret pair for a particular app.

Constructors

Token 

Instances

Instances details
FromJSON Token Source # 
Instance details

Defined in Network.Pusher.Data

data Address Source #

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: mt1, eu, ap1, ap2.

HostPort ByteString Word16

Used to connect to a raw address:port.

Main Pusher type

data Pusher Source #

The core handle to the Pusher API.

newPusher :: MonadIO m => Settings -> m Pusher Source #

Use this to get a Pusher instance.

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

trigger Source #

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.

triggerBatch Source #

Arguments

:: MonadIO m 
=> Pusher 
-> [Event]

The list of events to trigger.

-> m (Either PusherError ()) 

Trigger multiple events.

Event type for triggerBatch

data Event Source #

Constructors

Event 

Fields

Instances

Instances details
ToJSON Event Source # 
Instance details

Defined in Network.Pusher.Data

Channel queries

channels Source #

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.

channel Source #

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.

InvalidResponse Text

Received unexpected data from Pusher.

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.

data WebhookEv Source #

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.

Fields

ChannelVacatedEv

A channel has become vacated. There are 0 subscribers.

Fields

MemberAddedEv

A new user has subscribed to a presence channel.

Fields

MemberRemovedEv

A user has unsubscribed from a presence channel.

Fields

ClientEv

A client has sent a named client event with some json body. They have a socket_id and a User if they were in a presence channel.

Instances

Instances details
FromJSON WebhookEv Source # 
Instance details

Defined in Network.Pusher.Webhook

Show WebhookEv Source # 
Instance details

Defined in Network.Pusher.Webhook

Eq WebhookEv Source # 
Instance details

Defined in Network.Pusher.Webhook

data WebhookPayload Source #

Constructors

WebhookPayload 

Fields

data Webhooks Source #

A Webhook is received by POST request from Pusher to notify your server of a number of WebhookEvs. Multiple events are received under the same timestamp if batch events is enabled.

Constructors

Webhooks 

Instances

Instances details
FromJSON Webhooks Source # 
Instance details

Defined in Network.Pusher.Webhook

Show Webhooks Source # 
Instance details

Defined in Network.Pusher.Webhook

Eq Webhooks Source # 
Instance details

Defined in Network.Pusher.Webhook

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.