Flask-Pusher
Flask extension for Pusher. It is a thin wrapper around the official client, binding Flask app to Pusher client.
Installation
Install Flask-Pusher module from PyPI.
pip install Flask-Pusher
Configuration
The basic configuration for Pusher is app_id, key and secret. These values are available in Pusher web interface.
PUSHER_APP_ID = 'your-pusher-app-id'
PUSHER_KEY = 'your-pusher-key'
PUSHER_SECRET = 'your-pusher-secret'
You can connect to a custom host/port, with these configurations:
PUSHER_HOST = 'api.pusherapp.com'
PUSHER_PORT = '80'
The extension auto configure the Pusher encoder to use the app.json_encoder.
Usage
This extension simplify Pusher configuration and bind the client to your app.
from flask import Flask
from flask_pusher import Pusher
app = Flask(__name__)
pusher = Pusher(app)
# Use pusher = Pusher(app, url_prefix="/yourpath") to mount the plugin in another path
The extension gives you two ways to access the pusher client:
# you can just get the client from current_app
client = current_app.extensions["pusher"]
# or you can get it from the extension
client = pusher.client
In both cases, it is a reference to the pusher client.
client.trigger('channel_name', 'event', {
'message': msg,
})
Check the docs for the Pusher python client here: http://pusher.com/docs/server_api_guide#/lang=python
Pusher authentication
Pusher has authenticated private and presence channels. Flask-Pusher create
the /pusher/auth route to handle it. To support these authenticated routes,
just decorate a function with @pusher.auth.
This function must return True for authorized and False for unauthorized
users. It happens in the request context, so you have all Flask features,
including for example the Flask-Login current user.
Set the PUSHER_AUTH configuration to change the auth endpoint. The default value is /auth.
from flask_login import current_user
@pusher.auth
def pusher_auth(channel_name, socket_id):
if 'foo' in channel_name:
# refuse foo channels
return False
# authorize only authenticated users
return current_user.is_authenticated()
It also transparently supports batch auth, based on pusher-js-auth: https://github.com/dirkbonhomme/pusher-js-auth`. The authentication function is called for each channel in the batch.
Read more about user authentication here: http://pusher.com/docs/authenticating_users
Pusher channel data
Presence channels require channel_data. Flask-Pusher send by default the
user_id with the socket_id, because it is a required field.
The @pusher.channel_data gives you a way to set other values. If a user_id
key is returned, it overrides the default user_id.
from flask_login import current_user
@pusher.channel_data
def pusher_channel_data(channel_name, socket_id):
return {
"name": current_user.name
}
Pusher webhooks
Pusher has webhooks to send websocket events to your server.
Flask-Pusher create the routes to handle these webhooks and validate the headers X-Pusher-Key and X-Pusher-Signature.
from flask import request
@pusher.webhooks.channel_existence
def channel_existence_webhook():
print request.json
@pusher.webhooks.presence
def presence_webhook():
print request.json
@pusher.webhooks.client
def client_webhook():
print request.json
The JSON request is documented in Pusher docs: http://pusher.com/docs/webhooks
These webhooks routes are mounted in /pusher/events/channel_existence, /pusher/events/presence and /pusher/events/client. Configure your Pusher app to send webhooks to these routes.
Disclaimer
This project is not affiliated with Pusher or Flask.
Metadata
Release files for Flask-Pusher 3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Flask-Pusher-3.0.tar.gz | 6.4 kB | Without provenance | Details for Flask-Pusher-3.0.tar.gz |
Release files / Flask-Pusher-3.0.tar.gz
| Download URL | Flask-Pusher-3.0.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37b60c86e698068b148042f55f03b6d50254df6a9a85af115b4d47c2913b5795
|
|
BLAKE2b-256 checksum How to use checksums |
824a257ed7602a31e36364775d9c7c4a2b4fdd3eaaf43107f7b5e4056a0dfeb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5
|
Security for Flask-Pusher 3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.