Channels Server

Implements the Django Message Broker Server.

class django_message_broker.server.channels_server.ChannelsServer(ip_address: str = '127.0.0.1', port: int = 5556)

Message server for Django Channels.

Implements a network message server for Django channels using zero message queue (ZMQ) to support communication between channels registered within different processes.

The server opens two sequential ports:

  • Data port (base port, default=5556): Transmission of data messages between the client and server.

  • Signalling port (base port + 1, default=5557): Transmission of signalling messages between the client and server.

class DataCommands

Create registry of data commands using decorators.

class SignallingCommands

Create registry of signalling commands using decorators.

start() None

Start the channels server.

Raises

ChannelsServerError – If the server cannot be started.

stop() None

Stop the channels server.

_receive_data(multipart_message: Union[_asyncio.Future, List]) None

Callback raised when a message is received on the data port. Parses the message to identify the relevant command and dispatches the messages to the relevant command handler.

If the “ack” property is set then the server will send a response to the client. There are three potential responses:

  • COMPLETE: The command completed successfully.

  • EXCEPTION: An exception occurred in the server.

  • COMMAND SPECIFIC: A specific response to the command generated by the server.

Parameters

multipart_message (Union[Future, List]) – Multipart message received on data port.

_subscribe(message: django_message_broker.server.data_message.DataMessage) None

Command handler for SUBCHANX: Subscribe to a channel.

Registers an endpoint as a subscriber to a channel. If the channel doesn’t exist then it creates a new channel.

Parameters

message (DataMessage) – Message received requesting subscription to the channel.

_unsubscribe(message: django_message_broker.server.data_message.DataMessage) None

Command handler for USUBCHAN: Unsubscribe to a channel.

Removes an endpoint from a channel.

Parameters

message (DataMessage) – Message received requesting subscription to the channel.

_send_to_channel(message: django_message_broker.server.data_message.DataMessage) None

Command handler for SENDCHAN: Send message to a channel.

Creates a new queue if one does not exist. Adds the message to the queue.

If the “ttl” property is set on the message then set the expiry time of the message.

Parameters

message (DataMessage) – Received data message.

_send_to_group(message: django_message_broker.server.data_message.DataMessage) None

Command handler for SENDGRPX: Send message to a group.

Sends a message to a group of channels. The method creates a quick copy of the data message and pushes it to each channel. The shallow copy share the body of the message between all copies to reduce encoding/decoding times and memory utilisation.

Parameters

message (DataMessage) – Received data message.

_receive_signalling(multipart_message: Union[_asyncio.Future, List]) None

Callback raised when a message is received on the signalling port. Parses the message to identify the relevant command and dispatches the messages to the relevant command handler.

If the “ack” property is set then the server will send a response to the client. There are three potential responses:

  • COMPLETE: The command completed successfully.

  • EXCEPTION: An exception occurred in the server.

  • COMMAND SPECIFIC: A specific response to the command generated by the server.

Parameters

multipart_message (Union[Future, List]) – Multipart message received on data port.

Raises
  • ImportError – Error raised if the multitpart message cannot be parsed to a valid data message.

  • MessageCommandUnknown – The command in the message is unknown.

_group_add(message: django_message_broker.server.signalling_message.SignallingMessage) None

Command handler for GROUPADD: Add channel to group.

Adds a channel to a group. Creates the group if it does not exist.

If the ttl property is set then sets the expiry time of the group (default is 1 day)

Parameters

message (DataMessage) – Received data message.

_group_discard(message: django_message_broker.server.signalling_message.SignallingMessage) None

Command handler for GROUPDIS: Remove channel from group.

Removes a channel from a group.

Parameters

message (DataMessage) – Received data message.

_flush_all(_: django_message_broker.server.signalling_message.SignallingMessage) None

Command handler for FLUSHXXX: Reset the server.

Remove all groups, subscriptions and messages from the server

Parameters

_ (SignallingMessage) – Received message (not used)

_print(_: django_message_broker.server.signalling_message.SignallingMessage) None

Command handler for PRINTXXX: Print server contents.

Prints the contents of the message store and group store on the terminal.

Parameters

_ (SignallingMessage) – Received message (not used)

_flush_queues() None

Periodic callback to flush queues where there are no subscribers or messages.

_flush_groups() None

Periodic callback to flush groups that have expired.