pyphoenix.socket

Socket and WebSocket transport implementation for PyPhoenix.

Classes

Socket([websocket, phoenix_app])

Represents a client connection to the Phoenix server.

WebSocketTransport([host, port])

WebSocket transport for handling client connections.

class pyphoenix.socket.Socket(websocket=None, phoenix_app=None)[source]

Represents a client connection to the Phoenix server.

Handles WebSocket communication, channel management, and message routing.

Parameters:

websocket (Optional[ServerConnection])

__init__(websocket=None, phoenix_app=None)[source]

Initialize a socket connection.

Parameters:
  • websocket (Optional[ServerConnection]) – The WebSocket connection (None for testing)

  • phoenix_app – Phoenix application instance for routing

async push(message)[source]

Send a message through the socket.

Parameters:

message (Message) – The message to send

Return type:

None

async channel(topic, params=None)[source]

Get or create a channel for the given topic.

Parameters:
  • topic (str) – The channel topic

  • params (Optional[dict[str, Any]]) – Optional parameters for the channel

Return type:

Channel

Returns:

Channel instance

async handle_websocket_message(raw_message)[source]

Handle incoming WebSocket message.

Parameters:

raw_message (str) – Raw JSON message from WebSocket

Return type:

None

async close()[source]

Close the socket connection.

Return type:

None

class pyphoenix.socket.WebSocketTransport(host='localhost', port=4000)[source]

WebSocket transport for handling client connections.

Parameters:
__init__(host='localhost', port=4000)[source]

Initialize WebSocket transport.

Parameters:
  • host (str) – Host to bind to

  • port (int) – Port to bind to

set_phoenix_app(app)[source]

Connect this transport to a Phoenix application.

Parameters:

app – Phoenix application instance

async start()[source]

Start the WebSocket server.

Return type:

None

async stop()[source]

Stop the WebSocket server.

Return type:

None

async handle_connection(websocket)[source]

Handle new WebSocket connection.

Parameters:

websocket (ServerConnection) – The WebSocket connection

Return type:

None