Thursday, March 17, 2022

How can I send a message from a flask route to a socket using flask-socketio

Flask

I have the following scenario I would like to implement:

  • User surfs to our website
  • User enters a bitcoin address.
  • A websocket is created to the server, passing the address.
  • The server registers a callback with Blocktrail
  • When the callback is triggered (a payment was seen by Blocktrail) we send a message back to the browser.
  • The page the user is browsing is updated to show the message recieved

I'm using webhooks from the Blocktrail API to "listen" to an event, being the reception of coins on an address.

Now, when the event happens, the API does a POST to my URL. This should send a message to the browser that is connected to my server with socket.io (such as 'payment seen on blockchain')

So the question is,

How can I send a message from a route to a socket using flask-socketio

Pseudo code:

@app.route('/callback/<address>')def callback(id): socketio.send('payment seen on blockchain')@socketio.on('address',address)def socketlisten(address): registerCallback(address)

Answer link : https://codehunter.cc/a/flask/how-can-i-send-a-message-from-a-flask-route-to-a-socket-using-flask-socketio


No comments:

Post a Comment