pyphoenix.pubsub

Local PubSub implementation for PyPhoenix.

Functions

get_pubsub()

Get or create the global PubSub instance.

Classes

PubSub()

Local publish-subscribe system for inter-process communication.

Subscription(topic, callback, pubsub)

Represents a subscription to a topic.

class pyphoenix.pubsub.Subscription(topic, callback, pubsub)[source]

Represents a subscription to a topic.

Parameters:
__init__(topic, callback, pubsub)[source]

Initialize a subscription.

Parameters:
  • topic (str) – The topic being subscribed to

  • callback (Callable) – The callback function to invoke for messages

  • pubsub (PubSub) – Reference to the PubSub instance

async unsubscribe()[source]

Unsubscribe from the topic.

Return type:

None

class pyphoenix.pubsub.PubSub[source]

Local publish-subscribe system for inter-process communication.

This is a simplified, single-node implementation that will later be extended for distributed operation.

__init__()[source]

Initialize the PubSub system.

async subscribe(topic, callback)[source]

Subscribe to a topic.

Parameters:
  • topic (str) – The topic to subscribe to (supports wildcards like “room:*”)

  • callback (Callable) – The callback function to invoke for messages

Return type:

Subscription

Returns:

Subscription object that can be used to unsubscribe

async unsubscribe(topic, callback)[source]

Unsubscribe from a topic.

Parameters:
  • topic (str) – The topic to unsubscribe from

  • callback (Callable) – The callback function to remove

Return type:

None

async publish(topic, message)[source]

Publish a message to all subscribers of a topic.

Parameters:
  • topic (str) – The topic to publish to

  • message (Any) – The message to send (can be any type)

Return type:

int

Returns:

Number of subscribers that received the message

async direct_publish(node_id, topic, message)[source]

Publish directly to a specific node.

For now, this is the same as regular publish since we only have one node. This method is a placeholder for future distributed functionality.

Parameters:
  • node_id (str) – Target node identifier (ignored in local mode)

  • topic (str) – The topic to publish to

  • message (Any) – The message to send

Return type:

None

async list_topics()[source]

List all active topics with subscribers.

Return type:

list[str]

Returns:

List of topic names

async subscriber_count(topic)[source]

Get the number of subscribers for a topic.

Parameters:

topic (str) – The topic to check

Return type:

int

Returns:

Number of subscribers

async shutdown()[source]

Shutdown the PubSub system and clear all subscriptions.

Return type:

None

pyphoenix.pubsub.get_pubsub()[source]

Get or create the global PubSub instance.

Return type:

PubSub