Custom Namespaces in ORMBridge
Purpose
Custom namespaces let you target real-time updates to specific clients instead of all clients listening to all events.
How It Works
Backend Setup
- Define a namespace resolver function:
python
def thread_namespace(instance, action):
# Return the thread ID as the namespace
return instance.thread.id
- Register the resolver with your model:
python
registry.register(
Message,
additional_namespace_resolvers=[thread_namespace]
)
Frontend Setup
Subscribe a LiveView to specific events:
javascript
const liveView = new LiveView({
customNamespace: thread.id // Only receive updates for this thread
})
Key Benefits
- Performance: Clients only process relevant events
- Scalability: Reduces network traffic in high-volume applications
- Flexibility: Multiple resolvers can be used for complex filtering
Important Note
Events are always broadcast on the default model namespace. The filtering happens when LiveViews subscribe to specific namespaces.
Example: Chat Application
Problem
Without custom namespaces, all clients receive all message updates regardless of which thread they're viewing.
Solution
- Define a namespace resolver on the Message model that returns the thread ID
- Have each LiveView subscribe only to events for its current thread
- Result: Clients only receive updates for the thread they're currently viewing