Routing#

The routing module provides utilities for organizing WebSocket routes and patterns, including route discovery and traversal.

Patterns#

chanx.routing.patterns.get_pattern_string_and_params(route: Any) → tuple[str, dict[str, str] | None]#

Extract pattern string and path parameters from a route object.

Handles different route pattern implementations to extract the URL pattern string and identified named path parameters.

Supports both Django-style path parameters (<type:name>) and regex patterns.

Parameters:

route -- The route object to extract pattern from.

Returns:

  • The cleaned URL pattern string

  • Dictionary of path parameters with their type/pattern info, or None if no parameters

Return type:

A tuple containing

chanx.routing.patterns.extract_path_parameters(pattern: str) → dict[str, str]#

Extract path parameters from a URL pattern string.

Supports Django, Starlette/FastAPI, and regex-style parameters.

Parameters:

pattern -- The URL pattern string to extract parameters from.

Returns:

Dictionary of parameter names to their types/patterns.

chanx.routing.patterns.DJANGO_PARAM_PATTERN = '(?<!\\?P)<(?:(\\w+):)?(\\w+)>'#

name> or <name>, not (?P<name>.

Type:

Pattern for Django path parameters like <type

chanx.routing.patterns.REGEX_PARAM_PATTERN = '\\(\\?P<([^>]+)>([^)]+)\\)'#

Pattern for Python regex named groups like (?P<name>pattern).

chanx.routing.patterns.STARLETTE_PARAM_PATTERN = '\\{(\\w+)(?::(\\w+))?\\}'#

type}.

Type:

Pattern for Starlette/FastAPI path parameters like {name} or {name

Discovery#

class chanx.routing.discovery.RouteInfo(path: str, handler: Any, base_url: str, consumer: type[ChanxWebsocketConsumerMixin], path_params: dict[str, str] | None = None, topic: type[Topic[Any]] | None = None, host: type[ChanxWebsocketConsumerMixin] | None = None)#

Unified WebSocket route information.

This class consolidates the functionality from the previous separate RouteInfo implementations that were duplicated across multiple modules.

path#

The URL path pattern for the WebSocket route.

Type:

str

handler#

The consumer or handler function for this route.

Type:

Any

base_url#

The base WebSocket URL (e.g., ws://domain.com).

Type:

str

path_params#

Dictionary of path parameters with their regex patterns.

Type:

dict[str, str] | None

consumer#

The WebSocket consumer class (optional).

Type:

type[chanx.core.websocket.ChanxWebsocketConsumerMixin]

property channel_path: str#

Get a channel path with {param} format for AsyncAPI specification.

This method is used by AsyncAPI generation to create user-friendly path representations.

class chanx.routing.discovery.RouteDiscovery#

Abstract base class for route discovery implementations.

This class defines the interface that framework-specific route discovery implementations must follow. It provides a consistent API for discovering WebSocket routes across different frameworks.

abstract discover_routes(base_url: str = 'ws://localhost:8000') → list[RouteInfo]#

Discover all available WebSocket routes.

Parameters:

base_url -- The base WebSocket URL to use for discovered routes.

Returns:

List of RouteInfo objects representing discovered routes.

abstract extract_routes_from_router(router: Any, prefix: str, routes: list[RouteInfo], base_url: str) → None#

Extract routes from a router object.

This method should be implemented by subclasses to handle framework-specific router objects.

Parameters:
  • router -- The router object to extract routes from.

  • prefix -- URL prefix accumulated so far.

  • routes -- List to store discovered RouteInfo objects.

  • base_url -- Base URL for WebSocket connections.

Traversal#

chanx.routing.traversal.traverse_middleware_stack(app: Any, prefix: str, routes: list[RouteInfo], base_url: str, extract_routes_fn: RouteExtractor) → None#

Generic middleware traversal function.

Recursively explores the middleware stack to find router instances and extract route information from them using the provided extraction function.

This is framework-agnostic and can be used by different routing implementations.

Parameters:
  • app -- The current application or middleware to traverse.

  • prefix -- URL prefix accumulated so far.

  • routes -- List to store discovered RouteInfo objects.

  • base_url -- Base URL for WebSocket connections.

  • extract_routes_fn -- Function to extract routes from router objects.