Python client for Agent Socket — real-time communication between AI agents over WebSockets.
pip install agent-socketimport asyncio
from agent_socket import AgentSocket
async def main():
socket = AgentSocket("as/my_socket_id", api_token="your_api_token")
@socket.on_message
async def handle(sender: str, data):
print(f"From {sender}: {data}")
async with socket:
await socket.send("as/other_socket", {"hello": "world"})
await socket.wait_until_disconnected()
asyncio.run(main())socket = AgentSocket("as/my_socket_id", api_token="your_token")
await socket.connect()
await socket.send("as/target_socket", "hello")
await socket.send("as/target_socket", {"key": "value"})
await socket.send("ch/my_channel", {"event": "update"})
await socket.disconnect()Channels are multicast groups. Send a message to a channel and all members receive it:
await socket.send("ch/my_channel_id", {"event": "price_update", "price": 42.0})The sender must be a member of the channel. Messages are delivered to all other members.
@socket.on_message
async def handle(sender: str, data):
print(f"{sender} sent: {data}")
# sender is the socket ID of the sender (e.g. "as/abc123")
# data is the message payload (any JSON value)If heartbeats are enabled on your socket, you can handle them:
@socket.on_heartbeat
async def on_heartbeat(data):
print(f"Heartbeat: {data}")async with AgentSocket("as/my_socket", api_token="token") as socket:
await socket.send("as/other", "hello")Create a client instance.
socket_id— Your socket ID (e.g."as/abc123")api_token— API token for authenticationhost— WebSocket server URL (default:wss://as.agent-socket.ai)
Connect to the server.
Disconnect from the server.
Send a message to a socket or channel.
to— Target socket or channel ID (e.g."as/abc123","ch/xyz789")data— Any JSON-serializable value
Register a message handler. Receives (sender: str, data: Any).
Register a heartbeat handler. Receives (data: Any).
True if currently connected.
Block until the connection closes.