Publish MQTT from a TouchDesigner client DAT
TL;DR. The MQTT Client DAT connects to protocol://host:port, using tcp, ssl, ws, or wss. n.subscribe('weather') listens. n.publish('temperature', b'23') sends.
TouchDesignerIntermediateTouchDesignerMQTT
What it does
Derivative's MQTT Client DAT page says the operator receives and sends data through MQTT servers, which it also calls brokers. TouchDesigner acts as a client. Another computer has to act as the MQTT server. After the connection, the client can subscribe, which means it tells the server it wants messages whose topic string matches, and it can publish, which means it sends messages with a topic so the server can forward them to clients that subscribed to that topic. The MQTT overview page says there is no history of messages kept.
The Network Address parameter, netaddress, should take the form <protocol>://<host>:<port>. The accepted protocol URIs are tcp, ssl, ws, and wss. Active, active, enables the connection. Specify ID, specifyid, lets you name the client with User Client ID. Otherwise the name is generated automatically and is unique for each connection. The Clean Session parameter, cleansession, says that if Specify ID is selected, the server will preserve any state information associated with the connection of that ID, such as subscriptions and delivery attempts. The page does not add a second sentence about what happens when the toggle is off.
Keep Alive Interval is in seconds, the maximum time to expect without communication. If nothing is sent in that time, a lightweight ping goes to the server instead. It can be set to 0 to avoid pings. Username and Password are for a broker that requires them. The page says MQTT servers that support the MQTT v3.1 protocol provide authentication and authorization that way. Verify Certificate enables TLS certificate verification against the broker. Reconnect attempts to reconnect.
The Python class is mqttclientDAT. isConnected is true when connected to a broker. n.subscribe('weather') subscribes to that topic. A list of topics is allowed. The qos keyword is 0 for at most once, 1 for at least once, and 2 for exactly once, and it defaults to 0. n.publish('temperature', b'23') publishes that payload. retain=True sets the message as the last known good retained message for the topic. n.unsubscribe('weather') drops a subscription. onConnect runs when the connection is established. onMessage(dat, topic, payload, qos, retained, dup) runs when content arrives. The received-data page says the DAT is limited to 100 messages by default, and Clamp Output can change that limit, including to unlimited. An Info CHOP on this DAT has connected and messages_pending.
Answer HTTP inside TouchDesigner with the Web Server DAT is the operator that answers HTTP on a port. This one is a client of an MQTT broker.
How to set it up
- Add an MQTT Client DAT. Turn Active on. Set Network Address to
<protocol>://<host>:<port>. Usetcp,ssl,ws, orwssas the protocol. The page does not print a default host or port. - Leave Specify ID off if a generated client name is enough. Turn it on and set User Client ID when the broker should keep state for that name. The Clean Session text says that, with Specify ID selected, the server preserves subscriptions and delivery attempts for that ID.
- Fill Username and Password only if the broker requires them. Turn Verify Certificate on when you want TLS certificate checks. Set Keep Alive Interval to 0 if you do not want the ping.
- From the operator, call
n.subscribe('weather'). Publish withn.publish('temperature', b'23'). Passqos=1for at least once, orretain=Trueto store the last known good value. ReadisConnectedbefore you treat a publish as delivered. - Put the reaction in
onMessage. The arguments are the DAT, the topic, the payload, qos, retained, and dup. The table keeps 100 messages unless you change Clamp Output.connectedandmessages_pendingare the Info CHOP channels named for this DAT.
Source: MQTT Client DAT and mqttclientDAT Class.
FAQ
Does TouchDesigner run the broker?
No. The page says TouchDesigner is the client and another computer needs to be the MQTT server. The overview page says the broker distributes messages and that no message history is kept.
What does Clean Session preserve?
The parameter text says that if Specify ID is selected, the server preserves state for that client ID, including subscriptions and delivery attempts. It does not describe the off state in a second sentence.
Which quality-of-service value is the default?
subscribe and publish both default qos to 0. The class page labels 0 as at most once, 1 as at least once, and 2 as exactly once.
Also on this rig
Original source: https://docs.derivative.ca/MQTT_Client_DAT