Flask extension for Pusher

Related tags

FlaskFlask-Pusher
Overview

Flask-Pusher

Build Status Coverage Status

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.

You might also like...
An extension to add support of Plugin in Flask.

An extension to add support of Plugin in Flask.

Flask-Rebar combines flask, marshmallow, and swagger for robust REST services.

Flask-Rebar Flask-Rebar combines flask, marshmallow, and swagger for robust REST services. Features Request and Response Validation - Flask-Rebar reli

Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development.
Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development.

Flask-Starter Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development. It has all the r

Brandnew-flask is a CLI tool used to generate a powerful and mordern flask-app that supports the production environment.

Brandnew-flask is still in the initial stage and needs to be updated and improved continuously. Everyone is welcome to maintain and improve this CLI.

Flask Project Template A full feature Flask project template.

Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS

A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask
A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask

Flask-Fastx Flask-Fastx is a Fast API style support for Flask. It Gives you MyPy types with the flexibility of flask. Compatibility Flask-Fastx requir

Flask-app scaffold, generate flask restful backend

Flask-app scaffold, generate flask restful backend

Flask pre-setup architecture. This can be used in any flask project for a faster and better project code structure.

Flask pre-setup architecture. This can be used in any flask project for a faster and better project code structure. All the required libraries are already installed easily to use in any big project.

flask-reactize is a boostrap to serve any React JS application via a Python back-end, using Flask as web framework.

flask-reactize Purpose Developing a ReactJS application requires to use nodejs as back end server. What if you want to consume external APIs: how are

Comments
  • Update flask_pusher.py

    Update flask_pusher.py

    Only make endpoints if its the first time making them, ignore "AssertionError: View function mapping is overwriting an existing endpoint function: pusher.auth" error.

    Sorry about all the updates, these are changes I've made manually for some time now but can't deploy to production like that. If you could do another release <3

    opened by ScriptProdigy 3
  • Support auth endpoint

    Support auth endpoint

    Private and presence channels require authentication. This extension can easily handle this requirement.

    @pusher.auth
    def f(channel_name, socket_id):
        # here you have access to Flask request context
        # return True if the user has a access to the channel_name
        return True
    
    enhancement 
    opened by iurisilvio 1
  • DeprecationWarning: Request.is_xhr

    DeprecationWarning: Request.is_xhr

    /home/travis/build/iurisilvio/Flask-Pusher/.tox/py37-E/lib/python3.7/site-packages/werkzeug/local.py:347: DeprecationWarning: Request.is_xhr is deprecated. Given that the X-Requested-With header is not a part of any spec, it is not reliable
      return getattr(self._get_current_object(), name)
    
    opened by iurisilvio 0
  • DeprecationWarning: inspect.getargspec()

    DeprecationWarning: inspect.getargspec()

    DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() or inspect.getfullargspec()
      argspec = inspect.getargspec(_pusher.Pusher.__init__)
    /home/travis/build/iurisilvio/Flask-Pusher/.tox/py37-D/lib/python3.7/site-packages/werkzeug/local.py:347: DeprecationWarning: Request.is_xhr is deprecated. Given that the X-Requested-With header is not a part of any spec, it is not reliable
      return getattr(self._get_current_object(), name)
    
    opened by iurisilvio 0
Releases(v0.2)
Owner
Iuri de Silvio
Iuri de Silvio
Flask app for deploying DigitalOcean droplet using Pulumi.

Droplet Deployer Simple Flask app which deploys a droplet onto Digital ocean. Behind the scenes there's Pulumi being used. Background I have been Terr

Ahmed Sajid 1 Oct 30, 2021
An extension to add support of Plugin in Flask.

An extension to add support of Plugin in Flask.

Doge Gui 31 May 19, 2022
Curso Desenvolvimento avançado Python com Flask e REST API

Curso Desenvolvimento avançado Python com Flask e REST API Curso da Digital Innovation One Professor: Rafael Galleani Conteudo do curso Introdução ao

Elizeu Barbosa Abreu 1 Nov 14, 2021
Python3🐍 webApp to display your current playing music on OBS Studio.

Spotify Overlay A Overlay to display on Obs Studio or any related video/stream recorder, the current music that is playing on your Spotify. Installati

carlitos 0 Oct 17, 2022
A flask template with Bootstrap 4, asset bundling+minification with webpack, starter templates, and registration/authentication.

cookiecutter-flask A Flask template for cookiecutter. (Supports Python ≥ 3.6) See this repo for an example project generated from the most recent vers

4.3k Jan 06, 2023
Are-You-OK is a Flask-based, responsive Web App to monitor whether the Internet Service you care about is still working.

Are-You-OK Are-You-OK is a Flask-based, responsive Web App to monitor whether the Internet Service you care about is still working. Demo-Preview Get S

Tim Qiu 1 Oct 28, 2021
This is a small notes web app, with python and flask microframework. Using sqlite3

Python Notes App. This is a small web application maked with flask-python for add notes easily and quickly. Dependencies. You can create a virtual env

Eduard 1 Dec 26, 2021
Brandnew-flask is a CLI tool used to generate a powerful and mordern flask-app that supports the production environment.

Brandnew-flask is still in the initial stage and needs to be updated and improved continuously. Everyone is welcome to maintain and improve this CLI.

brandonye 4 Jul 17, 2022
Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications.

Flask Sitemapper Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications. This allows you to create a nice and

6 Jan 06, 2023
A simple FastAPI web service + Vue.js based UI over a rclip-style clip embedding database.

Explore CLIP Embeddings in a rclip database A simple FastAPI web service + Vue.js based UI over a rclip-style clip embedding database. A live demo of

18 Oct 15, 2022
Flask Application Structure with MongoDB

This application aims to serve as a template for APIs that intend to use mongoengine and flask-restx

Tiago Franco 5 Jun 25, 2022
Flask extension for Pusher

Flask-Pusher Flask extension for Pusher. It is a thin wrapper around the official client, binding Flask app to Pusher client. Installation Install Fla

Iuri de Silvio 9 May 29, 2021
A python package for integrating ripozo with Flask

flask-ripozo This package provides a dispatcher for ripozo so that you can integrate ripozo with Flask. As with all dispatchers it is simply for getti

Vertical Knowledge 14 Dec 03, 2018
Getting Started with Docker and Flask

Getting-Started-with-Docker-and-Flask Introduction Docker makes it easier, simpler and safer to build, deploy and manage applications in a docker cont

Phylis Jepchumba 1 Oct 08, 2021
Seamlessly serve your static assets of your Flask app from Amazon S3

flask-s3 Seamlessly serve the static assets of your Flask app from Amazon S3. Maintainers Flask-S3 is maintained by @e-dard, @eriktaubeneck and @SunDw

Edd Robinson 188 Aug 24, 2022
a flask profiler which watches endpoint calls and tries to make some analysis.

Flask-profiler version: 1.8 Flask-profiler measures endpoints defined in your flask application; and provides you fine-grained report through a web in

Mustafa Atik 718 Dec 20, 2022
Python web-app (Flask) to browse Tandoor recipes on the local network

RecipeBook - Tandoor Python web-app (Flask) to browse Tandoor recipes on the local network. Designed for use with E-Ink screens. For a version that wo

5 Oct 02, 2022
Flask Boilerplate - Paper Kit Design | AppSeed

Flask Paper Kit Open-Source Web App coded in Flask Framework - Provided by AppSeed Web App Generator. App Features: SQLite database SQLAlchemy ORM Ses

App Generator 86 Nov 29, 2021
A web application made with Flask that works with a weather service API to get the current weather from all over the world.

Weather App A web application made with Flask that works with a weather service API to get the current weather from all over the world. Uses data from

Christian Jairo Sarmiento 19 Dec 02, 2022
Rubik's cube assistant on Flask webapp

webcube Rubik's cube assistant on Flask webapp. This webapp accepts the six faces of your cube and gives you the voice instructions as a response. Req

Yash Indane 56 Nov 22, 2022