Sending Instant Messages
Receiving messages that originated from an instant messenger client is nice to have, and certainly opens up a lot of possibilities for collecting data in realtime. But the true realtime power of this technology comes from the ability to both send and receive these messages.
Let’s expand on our existing application by taking the message that
we received and just printing it back to the client. In the
main.py file, make the following addition to the
XMPPHandler
class:
class XMPPHandler(BaseHandler):
def post(self):
# Parse the XMPP request
message = xmpp.Message(self.request.POST)
# Log it to the console
logging.info("XMMP sender: %s - body: %s" % (message.sender, message.body))
message.reply(message.body)
That additional line of code takes the message that was received and
calls the reply
method to send the body right back to
the sender. If you’d like to test this out on the server, go ahead and
deploy the code as it is now. Figure 7-12 shows a typical
chat session with the server after adding this functionality.
Figure 7-11. The lifetime of the instant message
Figure 7-12. Chatting with myself via the server
Get Building the Realtime User Experience now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.