Skip to content Skip to sidebar Skip to footer

To Implement A Web Socket Feature With Flask-Restful (REST Api) On The Server Side

WORK DONE: I have implemented a REST API with Mongo DB (PyMongo driver) using Flask-Restful having one endpoint named “Users” consisting of GET, POST, PUT, DELETE My PUT method

Solution 1:

This should be interesting... Flask or Django or similar frameworks are built to serve HTTP request based method.

Imagine

  1. user click the button
  2. Your framework take the request
  3. do the needs
  4. and finally return the response

This is the actual flow of web server. but in your case you may want to update the frontend when DB updated or Any event changes.. In this case you need Socket to communicate with frontend.

Features of web sockets

  • Communicate with your website whenever you need.

Just put the javascript in the html page like this

$(document).ready(function(){
    var socket = io.connect('http://localhost:8000/test');
});

And now you are connected with Website so next in your python code..

@socketio.on('my event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']})

just like this import emit and put your message and you are good to go. For more detail please look here http://flask-socketio.readthedocs.org/en/latest/


Post a Comment for "To Implement A Web Socket Feature With Flask-Restful (REST Api) On The Server Side"