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")