Warning:
Fauna is decommissioning FQL v4 on June 30, 2025.

This driver is not compatible with FQL v10, the latest version. Fauna accounts created after August 21, 2024 must use FQL v10.
Ensure you migrate existing projects to the official v10 driver by the v4 EOL date: https://github.com/fauna/fauna-python.

For more information, see the v4 end of life (EOL) announcement and related FAQ.

Module faunadb.streams.subscription

Expand source code
from .client import Connection
from .dispatcher import EventDispatcher


class Subscription(object):
    """
    A stream subscription which dispatches events received to the registered
    listener functions. This class must be constructed via the FaunaClient stream
    method.
    """
    def __init__(self, client, expression, options=None):
        self._client = Connection(client, expression, options)
        self._dispatcher = EventDispatcher()

    def start(self):
        """
        Initiates the underlying subscription network calls.
        """
        self._client.subscribe(self._dispatcher.dispatch)

    def on(self, event_type, callback):
        """
        Registers a callback for a specific event type.
        """
        self._dispatcher.on(event_type, callback)

    def close(self):
        """
        Stops the current subscription and closes the underlying network connection.
        """
        self._client.close()

    def __repr__(self):
        return "stream:Subscription(state=%s, expression=%s, options=%s)"%(self._client._state,
            self._client._query,self._client._options)

Classes

class Subscription (client, expression, options=None)

A stream subscription which dispatches events received to the registered listener functions. This class must be constructed via the FaunaClient stream method.

Expand source code
class Subscription(object):
    """
    A stream subscription which dispatches events received to the registered
    listener functions. This class must be constructed via the FaunaClient stream
    method.
    """
    def __init__(self, client, expression, options=None):
        self._client = Connection(client, expression, options)
        self._dispatcher = EventDispatcher()

    def start(self):
        """
        Initiates the underlying subscription network calls.
        """
        self._client.subscribe(self._dispatcher.dispatch)

    def on(self, event_type, callback):
        """
        Registers a callback for a specific event type.
        """
        self._dispatcher.on(event_type, callback)

    def close(self):
        """
        Stops the current subscription and closes the underlying network connection.
        """
        self._client.close()

    def __repr__(self):
        return "stream:Subscription(state=%s, expression=%s, options=%s)"%(self._client._state,
            self._client._query,self._client._options)

Methods

def close(self)

Stops the current subscription and closes the underlying network connection.

Expand source code
def close(self):
    """
    Stops the current subscription and closes the underlying network connection.
    """
    self._client.close()
def on(self, event_type, callback)

Registers a callback for a specific event type.

Expand source code
def on(self, event_type, callback):
    """
    Registers a callback for a specific event type.
    """
    self._dispatcher.on(event_type, callback)
def start(self)

Initiates the underlying subscription network calls.

Expand source code
def start(self):
    """
    Initiates the underlying subscription network calls.
    """
    self._client.subscribe(self._dispatcher.dispatch)