Settings Reference

Chanx can be configured through Django's settings module. All Chanx settings are contained within a single CHANX dictionary in your project's settings file.

Configuration Options

Here's an overview of all available settings and their default values:

# settings.py
CHANX = {
    'MESSAGE_ACTION_KEY': 'action',
    'SEND_COMPLETION': False,
    'SEND_MESSAGE_IMMEDIATELY': True,
    'SEND_AUTHENTICATION_MESSAGE': True,
    'CAMELIZE': False,
    'LOG_RECEIVED_MESSAGE': True,
    'LOG_SENT_MESSAGE': True,
    'LOG_IGNORED_ACTIONS': ['ping', 'pong'],
    'WEBSOCKET_BASE_URL': 'ws://localhost:8000',
}

Settings Details

Setting

Default

Description

MESSAGE_ACTION_KEY

"action"

Key name for the action field in messages, used as the discriminator for message types

SEND_COMPLETION

False

Whether to send completion messages after processing a client message, should set it to True for testing and False for the other environments.

SEND_MESSAGE_IMMEDIATELY

True

Whether to send message immediately rather than wait to the end of the processing to send together.

SEND_AUTHENTICATION_MESSAGE

True

Whether to send authentication status message after connection authentication

CAMELIZE

False

Whether to convert message keys to camelCase format using pyhumps. Requires the pyhumps package to be installed.

LOG_RECEIVED_MESSAGE

True

Whether to log received WebSocket messages

LOG_SENT_MESSAGE

True

Whether to log sent WebSocket messages

LOG_IGNORED_ACTIONS

[]

List of message actions that should not be logged (e.g., frequent messages like heartbeats)

WEBSOCKET_BASE_URL

"ws://localhost:8000"

Default WebSocket URL for discovery and playground usage

Using Settings in Code

Chanx's settings are accessible through the chanx_settings object:

from chanx.settings import chanx_settings

# Access a setting
action_key = chanx_settings.MESSAGE_ACTION_KEY

# Check if completion messages are enabled
if chanx_settings.SEND_COMPLETION:
    # Do something

Overriding Settings in Tests

Chanx provides utilities for temporarily overriding settings in tests:

from chanx.utils.settings import override_chanx_settings, settings_context

# Using decorator for a test function
@override_chanx_settings(SEND_COMPLETION=True)
async def test_completion_message():
    # SEND_COMPLETION will be True within this test
    ...

# Using context manager
async def test_with_custom_settings():
    with settings_context(SEND_AUTHENTICATION_MESSAGE=False):
        # SEND_AUTHENTICATION_MESSAGE will be False within this block
        ...

Default Settings Source

The default settings are defined in chanx.settings.MySetting as a dataclass:

 1@dataclass
 2class MySetting:
 3    """
 4    Configuration settings for Chanx websocket framework.
 5
 6    This dataclass defines all available configuration options with their default values.
 7    These settings can be overridden through Django's settings with the CHANX dictionary.
 8
 9    Attributes:
10        MESSAGE_ACTION_KEY: Key name for action field in messages (default: "action")
11        SEND_COMPLETION: Whether to send completion messages after processing (default: False)
12        SEND_MESSAGE_IMMEDIATELY: Whether to yield control after sending messages (default: True)
13        SEND_AUTHENTICATION_MESSAGE: Whether to send auth status after connection (default: True)
14        CAMELIZE: Whether to convert keys to camelCase in messages (default: False)
15        LOG_RECEIVED_MESSAGE: Whether to log received messages (default: True)
16        LOG_SENT_MESSAGE: Whether to log sent messages (default: True)
17        LOG_IGNORED_ACTIONS: Message actions that should not be logged (default: empty list)
18        WEBSOCKET_BASE_URL: Default WebSocket URL for discovery (default: "ws://localhost:8000")
19        user_settings: Internal field used by APISettings
20    """
21
22    MESSAGE_ACTION_KEY: str = "action"
23    SEND_COMPLETION: bool = False
24    SEND_MESSAGE_IMMEDIATELY: bool = True
25    SEND_AUTHENTICATION_MESSAGE: bool = True
26    CAMELIZE: bool = False
27
28    LOG_RECEIVED_MESSAGE: bool = True
29    LOG_SENT_MESSAGE: bool = True
30    LOG_IGNORED_ACTIONS: Iterable[str] = dataclasses.field(default_factory=list[str])
31
32    WEBSOCKET_BASE_URL: str = "ws://localhost:8000"
33
34    # Add this field to satisfy the type checker
35    # It will be used by APISettings but isn't part of the real dataclass structure
36    user_settings: dict[str, Any] = dataclasses.field(default_factory=dict[str, Any])