Working with bi-directional communications
We can easily add a few lines of code to publish a message to the same channel in which we are receiving messages to indicate that the command has been successfully processed. We will use our previous example as a baseline and we will create a new version of the MessageChannel
class. The code file was iot_python_chapter_09_02.py
. Don't forget to replace the strings assigned to the publish_key
and subscribe_key
local variables in the __init__
method with the values you have retrieved from the previously explained PubNub key generation process. The following lines show the new version of the MessageChannel
class that publishes a message after a command has been successfully processed. The code file for the sample is iot_python_chapter_09_03.py
.
import time
from pubnub import Pubnub
class MessageChannel:
command_key = "command"
successfully_processed_command_key = "successfully_processed_command"
def __init__(self, channel, temperature_servo...