Search code examples
pythonajaxrestsocket.ioflask-socketio

Flask Rest API: how to switch to using sockets from AJAX?


I've been trying websocket-client and socketio-client with no luck so far. The broad picture of what I want to accomplish is this:

Currently, I have a Flask Rest API that has both a web front-end and a command line interface, and it handles several different sets of file uploads/downloads. Both communicate with the server using HTTP requests, the web one from JQuery AJAX and the CLI uses python requests. I would like to switch to using sockets so that database changes from one client appear on all of them. I have been able to get Flask-SocketIO working between my JQuery and Flask server, but I'm struggling with getting any client libraries working from the CLI portion. Is there an easy to use python library for sockets similar to requests I should be using for this transition, or am I going in a totally wrong direction with making this switch?

Another option, unsure of the viability, would be to try and keep both the REST API for the CLI and have sockets for the web interface. Sounds very messy though.


Solution

  • After doing a lot of searching and messing around with various libraries, the one that was easiest to get up and running connecting a command line tool with a Flask-SocketIO webapp was socketIO-client.

    This repository came in handy for the issues where I was struggling to understand how to correctly use the waits to receive info on the client side.

    Once I've finished the project in a few weeks, I will come back and add more details so people finding this in the future can have an easier time getting this set up.