Pysher
Pysher is a Python module for handling Pusher websockets, based on Erik Kulyk's PythonPusherClient. It provides client-side functionality to connect to Pusher channels, subscribe to events, and bind callbacks to handle real-time data. The project is currently in maintenance mode, welcoming PRs for fixes, updates, and features, with releases occurring periodically for merged improvements.
Warnings
- breaking Prior to v1.0.7, callback functions bound to events or connection binds might have received an incorrect number of arguments. Version 1.0.7 fixed this to match the expected argument signature, which could break existing code that relied on the old, incorrect argument count for `my_func(*args, **kwargs)` or similar callbacks.
- gotcha Pysher has specific compatibility requirements with its `websocket-client` dependency. `websocket-client==0.49` is explicitly excluded in `setup.py`, and issues have been reported with `websocket-client==0.58.0` and `Pysher 1.0.6`. Always refer to the `install_requires` in `setup.py` or the official GitHub README for the exact compatible versions.
- gotcha Pysher dropped support for Python versions prior to 3.5. Attempting to use Pysher on older Python 3 releases (e.g., 3.4 or 2.x) will result in compatibility errors.
- gotcha The Pysher library is explicitly stated to be in 'maintenance mode'. This implies that while bug fixes and contributions (PRs) are welcome and released, active development of new features or rapid updates should not be expected.
- gotcha For optimal performance, especially when handling high volumes of messages, it is recommended to install the `wsaccel` package. This allows the underlying `websocket-client` to utilize C-compiled UTF-8 validation methods, which are significantly more CPU-efficient than the default pure Python implementation.
Install
-
pip install pysher
Imports
- Pusher
import pysher pusher = pysher.Pusher(...)
Quickstart
import pysher
import os
import time
import logging
# Configure logging to see Pysher's internal communication
root = logging.getLogger()
root.setLevel(logging.INFO)
ch = logging.StreamHandler()
root.addHandler(ch)
# Replace with your actual Pusher App Key and Cluster
APP_KEY = os.environ.get('PUSHER_APP_KEY', 'your_app_key')
CLUSTER = os.environ.get('PUSHER_CLUSTER', 'mt1') # e.g., 'mt1', 'eu', 'ap1'
if APP_KEY == 'your_app_key':
print("WARNING: Please set PUSHER_APP_KEY environment variable or replace 'your_app_key' in the code.")
def my_func(*args, **kwargs):
"""Callback function to process messages from a subscribed event."""
print(f"Processing Args: {args}")
print(f"Processing Kwargs: {kwargs}")
def connect_handler(data):
"""Callback function when connection is established."""
print(f"Connected to Pusher! Connection data: {data}")
# Subscribe to a channel after successful connection
channel = pusher.subscribe('my-channel')
# Bind a callback to a specific event on that channel
channel.bind('my-event', my_func)
print("Subscribed to 'my-channel' and bound 'my-event'.")
# Initialize Pusher client
pusher = pysher.Pusher(APP_KEY, cluster=CLUSTER)
# Bind the connect handler to the 'pusher:connection_established' event
pusher.connection.bind('pusher:connection_established', connect_handler)
# Connect to Pusher
print("Connecting to Pusher...")
pusher.connect()
try:
while True:
# Keep the main thread alive to allow websocket client to run
time.sleep(1)
except KeyboardInterrupt:
print("\nDisconnecting Pysher.")
pusher.disconnect()
print("Disconnected.")