{"id":13421636,"url":"https://github.com/iurisilvio/Flask-Pusher","last_synced_at":"2025-03-15T10:31:15.819Z","repository":{"id":23814693,"uuid":"27191208","full_name":"iurisilvio/Flask-Pusher","owner":"iurisilvio","description":"Flask extension for Pusher","archived":false,"fork":false,"pushed_at":"2021-03-19T10:44:11.000Z","size":46,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-08T17:39:52.160Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iurisilvio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-26T18:47:17.000Z","updated_at":"2024-10-16T11:42:39.000Z","dependencies_parsed_at":"2022-08-22T05:31:20.080Z","dependency_job_id":null,"html_url":"https://github.com/iurisilvio/Flask-Pusher","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iurisilvio%2FFlask-Pusher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iurisilvio%2FFlask-Pusher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iurisilvio%2FFlask-Pusher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iurisilvio%2FFlask-Pusher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iurisilvio","download_url":"https://codeload.github.com/iurisilvio/Flask-Pusher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243718916,"owners_count":20336590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T23:00:27.471Z","updated_at":"2025-03-15T10:31:15.812Z","avatar_url":"https://github.com/iurisilvio.png","language":"Python","funding_links":[],"categories":["介绍","Other SDK"],"sub_categories":[],"readme":"Flask-Pusher\n============\n[![Build Status](https://travis-ci.org/iurisilvio/Flask-Pusher.svg?branch=master)](https://travis-ci.org/iurisilvio/Flask-Pusher)\n[![Coverage Status](https://coveralls.io/repos/iurisilvio/Flask-Pusher/badge.png?branch=master)](https://coveralls.io/r/iurisilvio/Flask-Pusher?branch=master)\n\n\nFlask extension for Pusher. It is a thin wrapper around the official client,\nbinding Flask app to Pusher client.\n\nInstallation\n------------\n\nInstall `Flask-Pusher` module from PyPI.\n\n```\npip install Flask-Pusher\n```\n\nConfiguration\n-------------\n\nThe basic configuration for Pusher is `app_id`, `key` and `secret`. These values are available in Pusher web interface.\n\n```python\nPUSHER_APP_ID = 'your-pusher-app-id'\nPUSHER_KEY = 'your-pusher-key'\nPUSHER_SECRET = 'your-pusher-secret'\n```\n\nYou can connect to a custom host/port, with these configurations:\n\n```python\nPUSHER_HOST = 'api.pusherapp.com'\nPUSHER_PORT = '80'\n```\n\nThe extension auto configure the Pusher encoder to use the `app.json_encoder`.\n\nUsage\n-----\n\nThis extension simplify Pusher configuration and bind the client to your app.\n\n```python\nfrom flask import Flask\nfrom flask_pusher import Pusher\n\napp = Flask(__name__)\npusher = Pusher(app)\n# Use pusher = Pusher(app, url_prefix=\"/yourpath\") to mount the plugin in another path\n```\n\nThe extension gives you two ways to access the pusher client:\n\n```python\n# you can just get the client from current_app\nclient = current_app.extensions[\"pusher\"]\n\n# or you can get it from the extension\nclient = pusher.client\n```\n\nIn both cases, it is a reference to the pusher client.\n\n```\nclient.trigger('channel_name', 'event', {\n    'message': msg,\n})\n```\n\nCheck the docs for the Pusher python client here: http://pusher.com/docs/server_api_guide#/lang=python\n\nPusher authentication\n---------------------\n\nPusher has authenticated private and presence channels. `Flask-Pusher` create\nthe `/pusher/auth` route to handle it. To support these authenticated routes,\njust decorate a function with `@pusher.auth`.\n\nThis function must return `True` for authorized and `False` for unauthorized\nusers. It happens in the request context, so you have all `Flask` features,\nincluding for example the `Flask-Login` current user.\n\nSet the `PUSHER_AUTH` configuration to change the auth endpoint. The default value is `/auth`.\n\n```python\nfrom flask_login import current_user\n\n@pusher.auth\ndef pusher_auth(channel_name, socket_id):\n    if 'foo' in channel_name:\n        # refuse foo channels\n        return False\n    # authorize only authenticated users\n    return current_user.is_authenticated()\n```\n\nIt 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.\n\nRead more about user authentication here: http://pusher.com/docs/authenticating_users\n\n\nPusher channel data\n-------------------\n\nPresence channels require `channel_data`. `Flask-Pusher` send by default the\n`user_id` with the `socket_id`, because it is a required field.\n\nThe `@pusher.channel_data` gives you a way to set other values. If a `user_id`\nkey is returned, it overrides the default `user_id`.\n\n```python\nfrom flask_login import current_user\n\n@pusher.channel_data\ndef pusher_channel_data(channel_name, socket_id):\n    return {\n        \"name\": current_user.name\n    }\n```\n\nPusher webhooks\n---------------\n\nPusher has webhooks to send websocket events to your server.\n\nFlask-Pusher create the routes to handle these webhooks and validate the headers `X-Pusher-Key` and `X-Pusher-Signature`.\n\n```python\nfrom flask import request\n\n@pusher.webhooks.channel_existence\ndef channel_existence_webhook():\n    print request.json\n\n@pusher.webhooks.presence\ndef presence_webhook():\n    print request.json\n\n@pusher.webhooks.client\ndef client_webhook():\n    print request.json\n```\n\nThe JSON request is documented in Pusher docs: http://pusher.com/docs/webhooks\n\nThese 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.\n\n\nDisclaimer\n----------\nThis project is not affiliated with Pusher or Flask.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiurisilvio%2FFlask-Pusher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiurisilvio%2FFlask-Pusher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiurisilvio%2FFlask-Pusher/lists"}