I am currently working on extending my existing REST API created using Flask-RESTPlus with WebSocket support. The idea is to create a Web Thing Model compliant Web Thing (Gateway). The "Things" in my use-case are dynamically added or removed.
The current setup allows a consumer to fetch the latest values from a Thing, e.g. temperature sensor, using a HTTP GET request to /thingId/properties/temperature. The values are in reality consumed from Kafka and temporarily stored in Redis.
Now I am wondering how I can extend this setup and allow the consumer to not only poll the latest values, but subscribe to the property of a Thing using WebSockets. I have a working solution where I create "Rooms" for each property, but this requires two separate servers and duplication of endpoints.
For REST I have
@app.route('/<thingId>/properties/<propertyId>')
# get latest datapoint
return latestDatapoint
For Flask-SocketIO I have
@socketio.on('join')
def on_join(data):
username = data['username']
room = data['room'] # e.g. /thingId/properties/temperature
join_room(room)
send(username + ' has entered the room.', room=room)
and then I'm forwarding the data to the correct room as it comes in from Kafka. On the client side I then need to connect to the WebSocket server and join the room
socket.on('connection', function(socket){
socket.emit('join', 'some room');
});
This implementation works, but I was strongly hoping for an alternative workflow as shown in the figure below where the client connects to the same endpoint used in the REST API, but with the WebSocket protocol instead of joining rooms etc.
Do you have any idea if this already exists or is feasible to implement?