question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Background thread how to use socketio push real time database info to client?

See original GitHub issue

This is my views file snippet:

thread = None


def background_thread(app=None):
    """
   send host status information to client
    """
    with app.app_context():
        while True:
            # update host info interval
            socketio.sleep(app.config['HOST_UPDATE_INTERVAL'])
            # socketio.sleep(5)
            all_hosts = dict([(host.id, host.status) for host in Host.query.all()])
            socketio.emit('update_host', all_hosts, namespace='/hostinfo')


@main.route('/', methods=['GET', 'POST'])
def index():
    all_hosts = Host.query.all()
    return render_template('dashboard.html', hosts=all_hosts, async_mode=socketio.async_mode)

@socketio.on('connect', namespace='/hostinfo')
def on_connect():
    global thread
    if thread is None:
        app = current_app._get_current_object()
        thread = socketio.start_background_task(target=background_thread, app=app)
    emit('my_response', {'data': 'conncted'})

@socketio.on('disconnect', namespace='/hostinfo')
def on_disconnect():
    print 'Client disconnected...', request.sid


@socketio.on('my_ping', namespace="/hostinfo")
def ping_pong():
    emit('my_pong')

I want to push real-time database Host table information to the client using a background thread which uses flask-socketio send the information. However, background thread uses current_app context, so this context is just a “snapshot”, so the database information is also just a “snapshot”, however, I want to get the real time database information just like in the app context. So how background thread use socketio push real time database info to client?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
miguelgrinbergcommented, Feb 21, 2017

@buweilv so the only problem is that you can’t get updated values from the database? Then try to clear the session, as that will eliminate all cached objects and force the next query to go to the database. Use db.session.remove().

0reactions
buweilvcommented, Feb 21, 2017

Ok , it’s not neccessary to add trouble to flask app, thanks 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why flask background thread get wrong database information?
In order to push real time database info to client, I use flask-socketio in server side by using websocket to push all real-time...
Read more >
HTML5 Push Notification System Using Nodejs MySQL Socket ...
Open PHPMyAdmin and create new Database name as socketDemo and then import the SQL file provided in Source code. This will create tables...
Read more >
Basic CRUD application - Socket.IO
While using Socket.IO (or plain WebSockets) for a basic CRUD application might sound a bit overkill, the ability to easily notify all users...
Read more >
How to Build a Real-time Chat App with React, Node, Socket ...
In this article, we will be using Socket.io and HarperDB to build a fullstack, real-time chat application with chat rooms.
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
First, open your terminal and create a new project directory that will hold both our server and client code: mkdir socket-example ... Next,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found