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
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access