Python and Web Page Interaction over STOMP

A Python STOMP Client for interaction with A Springboot Server

srinivas kumar r

--

STOMP Introduction

STOMP which stands for Simple Text Oriented Messaging Protocol is a sub protocol which runs on top of Websocket connections and is used for message exchanges between clients via an intermediate message broker.

STOMP has client implementations in different languages such as in Javascript(stomp-js) and Java, to interact with a Springboot Server over websockets. Do go through this amazing article to understand how Springboot over websockets with STOMP works.

Now let us go over building a python client with the required springboot server configurations for interactions between the two.

Required Server Side Configuration

Spring Server Configuration

Building the python client

We shall be building a class StompClient, which contains all the relevant methods for establishing the connection, subscribing to topics and receiving the messages. Please refer to the source code here.

Setting up the Connection

Websocket is the pipe through which the messages will flow between the server-client and vice versa. We want a persistent websocket connection, which is only terminated upon server termination or our interruption. The below snippet does that. Note that we pass multiple handler functions which handle the various events that occur upon connection.

Opening a websocket connection

Do note that the URL we connect to ws://<server_ip>:<port_number>/notifications/websocket, contains the extra websocket introduced due to the sockJs fallback. If you do not add this, you might hit a 200 OK instead of 101 which is expected for a successful websocket connection.

Upon the start of the connection, the client needs to explicitly send a CONNECT message to the server, which in return will add the client to it’s registries and reply back with a CONNECT_ACK. Note that sending this is mandatory, for the client to receive messages from the server, I have written extensively about it in this StackOverflow answer.

Handler invoked upon opening applications

Subscribing to the topics of interest

As we see in the above snippet, we can use the stomper module to create the SUBSCRIBE message for the required destination and send it across the websocket.

Receiving the Messages

We receive the messages in the handler we specified above i.e on_message. The message is unpacked and added to the notification queue.

Receiving a message

Running the client

In order to run the client all we need to do is, instantiate an object of the same and call the create_connection(). Note that this is blocking in a single threaded environment, due to the blocking nature of run_forever() method. If run in a separate thread we can have multiple clients subscribed to many destinations, whilst the main thread only reads the required notifications!

Stomp Python client runner

Conclusion

So with this article we shall be able to build a Stomp Python client which can be run in a single/multi threaded fashion, also we must take care of the niche mistakes as mentioned above which may result in client not receiving any messages! All the code can be found at

--

--

srinivas kumar r

Srinivas is a software developer with 3 years of experience developing Java Microservices, Python and currently a MSc student at University Of Stuttgart.