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:
def put(self, short_name ):
coll = db.users
user_info = request.get_json()
print user_info #for debugging
coll.update({'short_name': short_name'}, {"$set": user _info})
return Response(json_util.dumps(user _info), mimetype='application/json')
The above PUT method accepts a short_name, updates the User database and returns a response.
Work to be done: I need to implement a server side web socket feature where after coll.update({'short_name': short_name'}, {"$set": user _info})
is executed, a message to the client (i.e frontend which is built completely on angular) has to be sent stating “Data updated successfully”.
I saw a couple of snippets online but couldn’t find one with REST Api. For now I only require the server side implementation. I will try to figure out the client side implementation later. Any help on how to accomplish this is appreciated. Can it be done with simple python or is socket.io needed. All solutions are welcome.
EDIT: My modified server code
def put(self, short_name ):
coll = db.users
user_info = request.get_json()
print user_info #for debugging
coll.update({'short_name': short_name'}, {"$set": user _info})
emit('my response', {'data': 'update successful'})
return Response(json_util.dumps(user _info), mimetype='application/json')
Added this on the client side:
namespace = '/';
var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);
socket.on('my response', function(msg) {
console.log(msg.data)
});
Now when I run my application, the console on the client side does not print my data sent from the server. Can anyone explain where am I going wrong.