Broadcasting messages to WebSocket API clients
Invoking individual WebSocket connections has been supported since Zato 3.0 and Zato 3.1 adds new functionality on top of it - message broadcasting - which lets one notify all the clients connected to a particular channel. Here is how to use it.
Let’s say that there is a WebSocket channel such as the one here:
In the context of broadcast messages, the most important part of this definition is its name - below, we will be sending messages to all clients connected to that particular channel by its name.
# -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals # Zato from zato.server.service import Service class WSXBroadcast(Service): def handle(self): # Channel to invoke name = 'My WSX API Channel' # Get a handle to the channel object channel = self.wsx.channel[name] # Message to send data = 'Hello from Zato' # Broadcast this message to all clients connected to that channel channel.broadcast(data)
Note that the messages are always sent in background - they are treated as asynchronouous messages and Zato does not wait for any potential response from the clients.