Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Users online

Hello, I have to make endpoint that showing list of online users. Does anyone have some example code with best way to deal with sessions and users online with Phalcon.

Thank you



7.9k
Accepted
answer

a counter stored in db or a flag in users table you toggle when users log in/out, additionaly, a websocket can be listening at browser side so count is updated in real time.

Realtime user status is quite hard, you wont find any out-of-the box working example. The easiest would be to run an ajax request from the browser every N secods (30-60) and update a last onlne datetime column. Session variables are useless since you cant access it from other client sessions.



9.4k

Ok, I have to return only number of users online not list of them as I thinked before. This should be easier to make now ...

edited Jul '16

Your best bet here is probably web sockets pub/sub service. Building this in a phalcon call is probably adding to much overhead to somethign that should be fast and efficient. Phalcon, in my opinion, is not the tool for something like this. Phalcon is not by its nature real time, it is a post/response methodology.

If you need to store user online count over time then you can simply store those calls in the database with a post request from your pub/sub service periodically.

I don't like the idea of storing it on teh user table, this can get corrupted and incorrect really fast unless you write procedures to periodically check for stagnate users. Storing it in memory and updating off of "connections" would be a more accuarate and more reliable number.

Periodically posting via ajax, adds some additioanlly complexities on teh back end that you have to deal with. have to do diff calculations to find out who has fallen off since the last grace period.