2

I am building an application for live streaming. User can call another user if he/she is online. I can register log when user is Logged on. However I am searching for a way to check if user quits/exits the app. Any suggestion is welcome.

Dewsworld
  • 13,367
  • 23
  • 68
  • 104

4 Answers4

5

You can easily monitor this with Flask-SocketIO. The advantage of using this is that you can monitor this in real time. Below will be helpful:

Project directory

Project
|
|-> templates
|   |
|   |-> home.html
|   |-> login.html
|   |-> logout.html
|
|-> app.py

app.py

from flask import Flask, render_template
from flask_socketio import SocketIO, emit


app = Flask(__name__)
app.config['SECRET_KEY'] = 'SecretKey@123'
socket = SocketIO(app)


@app.route('/')
def index():
    return render_template('home.html')


@app.route('/login/<username>')
def login(username):
    '''
    When the user logs in.
    '''
    return render_template('login.html', username=username)


@app.route('/logout/<username>')
def logout(username):
    '''
    When the user logs out.
    '''
    return render_template('logout.html', username=username)


@socket.on('online')
def online(data):
    emit('status_change', {'username': data['username'], 'status': 'online'}, broadcast=True)


@socket.on('offline')
def online(data):
    emit('status_change', {'username': data['username'], 'status': 'offline'}, broadcast=True)


if __name__ == '__main__':
    socket.run(app)

home.html

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io();
    socket.on('status_change', function(data) {
        console.log('Status changed: ', data)
    });
</script>

login.html

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io();
    socket.emit('online', {'username': '{{username}}' });
</script>

logout.html

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io();
    socket.emit('offline', {'username': '{{username}}' });
</script>

Now, with the above code, you open the link, http://localhost:5000 in two tabs, in first tab, if you hit the link http://localhost:5000/login/A, then in the second tab's console you will see, Status changed: {username: "A", status: "online"}. Now, in first tab, if you hit the link http://localhost:5000/logout/A, then you will see Status changed: {username: "A", status: "offline"} in second tab's console.

I hope it was helpful.

ngShravil.py
  • 4,742
  • 3
  • 18
  • 30
  • 1
    what will happen if a user didn't hit logout URL and directly close browser? – Shailesh Bhokare Sep 09 '20 at 06:55
  • 1
    Nothing will happen, if you directly close the browser/tab. If you want to handle this type of scenario, then you can have look at [this](https://stackoverflow.com/a/62356752/6635464) answer. – ngShravil.py Sep 15 '20 at 14:59
3

1) You can implement a heartbeat system where the online users send messages to each other to ensure that they're alive

2) If by quitting you mean the user selects a designated "quit" button then you can just send a response to the other user saying you have quit

I'm sure there are a lot more ways to do it but these are the two ways that I've learned in my distributed system's course

Mark
  • 564
  • 4
  • 12
1

I think it depends on what your definition of "logged out" is in the context of your app. as user1736436 said, if it's just a question of whether they clicked the "log out" button, it's pretty simple. Of course, if the users internet stops, or the browser crashes, you won't know.

If you are using some other realtime connection, like websockets, you can define it to mean whether that socket is open or not. Websocket implementations often have some kind of status property that you can read on the server side (for example gevent-websocket sockets have a boolean attribute .closed that you can check)

If you go the heartbeat route, you'll probably end up triggering it in Javascript using a setInterval callback, and writing some kind of timed process on the server to take some action if you don't get one for a while.

domoarigato
  • 2,802
  • 4
  • 24
  • 41
0

To be able to do that you need to use an asynchronous connection for each user when he logs in. Check out Tornadi which is more suitable than flask for this kind of applications. But if you insist on using flask you'll have to figure out a way to implement the async part of the system using Tornado and making it coexist with Flask. You can also use Python's asyncio module if you would rather not use Tornado but you'll have to create everything from scratch which is in most cases not ideal.

Codejunky
  • 632
  • 1
  • 6
  • 15