Sending a message to all Clients

Even though we currently work with only oneclient, our local browser, there will be situations were we want to send the same message to every client at the same time. This is not to hard, as the WebServerDAT offers already a list of all the conected clients. (We have our table, but using the already existing list keeps our footprint smaller.)

First, lets create a new textDAT and lets name it broadcaster. In here, we define a function sendTextMessageToAll in which we loop over all theconected clients and send them the wanted data.

def sendTextMessageToAll(message):
	for client in op('webserver1').webSocketConnections:
		op('webserver1').webSocketSendText(client, message)

We can now acces the sendTextMessageToAll function via module on demand or short mod functionalityy of touchdesigner. Lets test it out by placing a new textDAT with the following content

mod.broadcaster.sendTextMessageToAll("Hello There")

Run the script and check the console in your browser. It should say “Hello There”.