pyphoenix.middleware

Middleware system for PyPhoenix channels and messages.

Classes

AuthMiddleware([auth_handler])

Middleware that handles authentication and authorization.

LoggingMiddleware([log_level])

Middleware that logs all channel activities.

Middleware()

Base class for middleware components.

MiddlewareStack([middleware])

Manages and executes middleware in order.

RateLimitMiddleware([max_messages_per_second])

Middleware that implements rate limiting.

class pyphoenix.middleware.Middleware[source]

Base class for middleware components.

abstract async process_join(payload, socket, next_handler)[source]

Process channel join requests.

Parameters:
Return type:

dict[str, Any]

abstract async process_message(event, payload, socket, next_handler)[source]

Process incoming messages.

Parameters:
Return type:

Any

abstract async process_leave(reason, socket, next_handler)[source]

Process channel leave events.

Parameters:
Return type:

None

class pyphoenix.middleware.LoggingMiddleware(log_level='info')[source]

Middleware that logs all channel activities.

Parameters:

log_level (str)

__init__(log_level='info')[source]
Parameters:

log_level (str)

async process_join(payload, socket, next_handler)[source]

Log join attempts and results.

Parameters:
Return type:

dict[str, Any]

async process_message(event, payload, socket, next_handler)[source]

Log message processing.

Parameters:
Return type:

Any

async process_leave(reason, socket, next_handler)[source]

Log leave events.

Parameters:
Return type:

None

class pyphoenix.middleware.AuthMiddleware(auth_handler=None)[source]

Middleware that handles authentication and authorization.

Parameters:

auth_handler (Optional[Callable[[dict[str, Any]], Any]])

__init__(auth_handler=None)[source]
Parameters:

auth_handler (Optional[Callable[[dict[str, Any]], Any]])

async process_join(payload, socket, next_handler)[source]

Authenticate join requests.

Parameters:
Return type:

dict[str, Any]

async process_message(event, payload, socket, next_handler)[source]

Check authorization for message events.

Parameters:
Return type:

Any

async process_leave(reason, socket, next_handler)[source]

Handle leave cleanup.

Parameters:
Return type:

None

class pyphoenix.middleware.RateLimitMiddleware(max_messages_per_second=10)[source]

Middleware that implements rate limiting.

Parameters:

max_messages_per_second (int)

__init__(max_messages_per_second=10)[source]
Parameters:

max_messages_per_second (int)

async process_join(payload, socket, next_handler)[source]

No rate limiting on joins.

Parameters:
Return type:

dict[str, Any]

async process_message(event, payload, socket, next_handler)[source]

Apply rate limiting to messages.

Parameters:
Return type:

Any

async process_leave(reason, socket, next_handler)[source]

Clean up rate limiting data on leave.

Parameters:
Return type:

None

class pyphoenix.middleware.MiddlewareStack(middleware=None)[source]

Manages and executes middleware in order.

Parameters:

middleware (Optional[list[Middleware]])

__init__(middleware=None)[source]
Parameters:

middleware (Optional[list[Middleware]])

add(middleware)[source]

Add middleware to the stack.

Parameters:

middleware (Middleware)

Return type:

None

remove(middleware)[source]

Remove middleware from the stack.

Parameters:

middleware (Middleware)

Return type:

None

async process_join(payload, socket, final_handler)[source]

Process join through all middleware.

Parameters:
Return type:

dict[str, Any]

async process_message(event, payload, socket, final_handler)[source]

Process message through all middleware.

Parameters:
Return type:

Any

async process_leave(reason, socket, final_handler)[source]

Process leave through all middleware.

Parameters:
Return type:

None