I have a website (Java + Spring) that relies on Websockets (Stomp over Websockets for Spring + RabbitMQ + SockJS) for some functionality.
We are creating a command line interface based in Python and we would like to add some of the functionality which is already available using websockets.
Does anyone knows how to use a python client so I can connect using the SockJS protocol ?
PS_ I am aware of a simple library which I did not tested but it does not have the capability to subscribe to a topic
PS2_ As I can connect directly to a STOMP at RabbitMQ from python and subscribe to a topic but exposing RabbitMQ directly does not feel right. Any comments around for second option ?
The solution I used was to not use the SockJS protocol and instead do "plain ol' web sockets" and used the websockets package in Python and sending Stomp messages over it using the stomper package. The stomper package just generates strings that are "messages" and you just send those messages over websockets using
ws.send(message)
Spring Websockets config on the server:
And on the Python client side of code:
Now
d
will be a Stomp formatted message, which has a pretty simple format. MSG is a quick and dirty class I wrote to parse it.This isn't the most complete solution. There isn't an unsubscribe and the id for the Stomp subscription is randomly generated and not "remembered." But, the stomper library provides you the ability to create unsubscribe messages.
Anything on the server side that is sent to
/something-to-subscribe-to
will be received by all the Python clients subscribed to it.I Have answered the particular question of sending a STOMP message from Springboot server with sockJs fallback to a Python client over websockets here: Websocket Client not receiving any messages. It also addresses the above comments of