PyFaye
An asynchronous Python client for the Faye publish-subscribe messaging protocol.
Features
- Asynchronous implementation using
asyncioandaiohttp/websockets - Support for both WebSocket and HTTP Long-Polling transports
- Automatic transport selection and fallback
- Extensible architecture with support for custom extensions
- Channel subscription management
- Message validation and channel validation
- Automatic reconnection with exponential backoff
- Comprehensive error handling and type hints
Installation
Install using pip:
pip install pyfaye
Or with Poetry:
poetry add pyfaye
Quick Start
import asyncio
from faye import FayeClient
async def main():
# Create a client (defaults to WebSocket transport)
client = FayeClient("http://your-faye-server/faye")
# Connect to server
await client.connect()
# Subscribe to a channel
async def message_handler(message):
print(f"Received message: {message.data}")
await client.subscribe("/some/channel", message_handler)
# Publish a message
await client.publish("/some/channel", {"message": "Hello, World!"})
# Disconnect when done
await client.disconnect()
if __name__ == "__main__":
asyncio.run(main())
Advanced Usage
Custom Extensions
from faye import Extension, Message
class SigningExtension(Extension):
def __init__(self, token: str):
self.token = token
async def process_outgoing(self, message: Message) -> Message | None:
"""Sign outgoing messages."""
if not message.ext:
message.ext = {}
message.ext["token"] = self.token
return message
# Create client with extension
client = FayeClient("http://your-faye-server/faye")
client.add_extension(SigningExtension("your-auth-token"))
await client.connect()
Message Batching
from faye import Message
# Create multiple messages
messages = [
Message("/channel/1", data={"seq": 1}),
Message("/channel/2", data={"seq": 2})
]
# Send messages in batch
responses = await client.batch(messages)
Transport Selection
# Use HTTP Long-Polling transport
client = FayeClient("http://your-faye-server/faye", transport_type="long-polling")
# Use WebSocket transport (default)
client = FayeClient("http://your-faye-server/faye", transport_type="websocket")
API Reference
FayeClient
The main client class for interacting with a Faye server.
Constructor
FayeClient(
url: str,
transport_type: str = "websocket",
extensions: list[Extension] | None = None
)
url: Faye server URLtransport_type: Transport type to use ("websocket" or "long-polling")extensions: Optional list of extensions to use
Properties
client_id: The client ID assigned by the server (read-only)connected: Whether the client is currently connected (read-only)state: Current connection state as string (read-only)
Methods
async connect() -> None: Connect to the Faye serverasync disconnect() -> None: Disconnect from the serverasync subscribe(channel: str, callback: Callable[[Message], Awaitable[None]]) -> None: Subscribe to a channelasync unsubscribe(channel: str) -> None: Unsubscribe from a channelasync publish(channel: str, data: dict[str, Any] | str) -> None: Publish data to a channelasync batch(messages: list[Message]) -> list[Message | None]: Send multiple messages in batchadd_extension(extension: Extension) -> None: Add an extension to the clientremove_extension(extension: Extension) -> None: Remove an extension from the client
Message
The message class representing Faye protocol messages.
Message(
channel: str,
client_id: str | None = None,
data: dict[str, Any] | str | None = None,
error: str | None = None,
ext: dict[str, Any] | None = None,
message_id: str | None = None,
version: str | None = None,
minimum_version: str | None = None,
supported_connection_types: list[str] | None = None,
connection_type: str | None = None,
subscription: str | None = None,
successful: bool | None = None,
advice: dict[str, Any] | None = None
)
Factory Methods
handshake(ext: dict[str, Any] | None = None) -> Messageconnect(client_id: str, connection_type: str = "websocket") -> Messagedisconnect(client_id: str) -> Messagesubscribe(client_id: str, subscription: str) -> Messageunsubscribe(client_id: str, subscription: str) -> Messagepublish(channel: str, data: dict[str, Any], client_id: str) -> Message
Development
Setup
# Clone the repository
git clone https://github.com/mwhobrey/pyfaye.git
cd pyfaye
# Install dependencies
poetry install
# Run tests
poetry run pytest
Running Tests
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=faye
# Run specific test file
poetry run pytest tests/transport/test_websocket.py
Code Quality
# Run all checks
poetry run prerelease
# Format code
poetry run black src/
# Run linter
poetry run ruff check src/
# Run type checker
poetry run pytype src/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. When contributing:
- Fork the repository
- Create a feature branch
- Add tests for any new functionality
- Ensure all tests pass and code quality checks succeed
- Submit a pull request
For bug reports or feature requests, please open an issue on GitHub.
Metadata
Release files for pyfaye 0.1.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 |
|---|---|---|---|---|
| pyfaye-0.1.0.tar.gz | 26.0 kB | Without provenance | Details for pyfaye-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfaye-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pyfaye-0.1.0-py3-none-any.whl |
Total release size: 56.2 kB
Release files / pyfaye-0.1.0.tar.gz
| Download URL | pyfaye-0.1.0.tar.gz |
|---|---|
| Size | 26.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ce0e3e875f8292825711159392ea84db3b20e92004f94ce8135d6710cbf6260
|
|
BLAKE2b-256 checksum How to use checksums |
cdfc59313bc254f896e83c1dd09a5a7c85c8fcadb8a640261d74e74d6d54ba39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.3 Windows/11
|
Release files / pyfaye-0.1.0-py3-none-any.whl
| Download URL | pyfaye-0.1.0-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a9257f062ed9f0830405650317dbb425258394a5ba7441873400308674964c30
|
|
BLAKE2b-256 checksum How to use checksums |
d99db4a83f7bbd8428f99e4f794b5d32f53eeb510f6c7ae7d8a311f605a09eaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.12.3 Windows/11
|
Security for pyfaye 0.1.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.