Laravel : Web Api, how to find out if a user (application) is online? - laravel

I am building a web service for mobile application by Laravel 5.8. I need to get the location of online users, search on some properties and send a notification to them.
The problem is I can't find out if a user is online. My solution was to keep pushing/receiving a message to applications every 10 seconds once they get connected for the first time. Clearly this approach has a huge overload on the server and is not practical.
I would be appreciated for any help.
Thanks in advance.

Sounds like you may make use of Real-time connections such as Pusher
You would need presence channels to see who is online in JS, it works more client to client rather than the Laravel server knowing who is online.
You subscribe users to a channel and then you can see who else is on that channel.
https://laravel.com/docs/5.8/broadcasting#presence-channels

Why not make a column to your user table namely 'is_online' ? So every time the user logged in set this field to true ? and when they are logged out or inactive for a specific of time you will set it to false ?

Related

How to get online users(in real time)

I have a task to display a list of online users present on the site. I've been said that I should use web-sockets for this.
I don't know how to implement this. I've already installed Pusher and Laravel Echo. Could you help me by pointing in right direction? I guess I should listen for the moment when user socket connects.
Reading all the comments I've imagined two possible solutions:
Broadcast when a user logs in / out
This is the very simple solution. You can use laravel notifications that allows you to implement a specific broadcast logic:
During the login / logout phase create a UserLogged[In|Out] notification and broadcast it to a specific channel
Using Laravel Echo and a "few lines" of javascript, update your onlineUsers list (for example in VueJs you may want to update the Vuex store)
But this may require a more complex logic and you may have to write a lot of code to keep the online users list updated... I think it's better to move to solution #2...
Use presence channels
I think this is the best way, since it doesn't require nothing else that a good Laravel-echo configuration. For the example I'm using Pusher (since you asked for), but this solution can be implemented with all the Laravel broadcast drivers.
When a user logs in, simply subscribe him / her to a presence channel (here the Laravel documentation)
Using Laravel Echo, subscribe the user to that channel
var chatRoom = Echo.join('my.chat.room.id');
Once subscribed use themembers.count method to keep an updated list of the channel users.
Using a setTimeout() function you can have a realtime update of the users list.
Notes
I've never implemented this solutions in a production environment, but just wrote some code to see how it works. so I think that if other people have diffent point of view or have a better way to explain this process I will be happy to update my answer.

Use Nchan to post to logged in users only

I am setting up an application with realtime events using nchan (https://github.com/slact/nchan). The application itself is built on the Laravel framework. However, for now everyone that subscripes to specific sub-channel will get new events as soon infomation is pushed to the corresponing pub-channel.
I want to limit that so only logged in users should be able to subscribe to events. Hence, if I am not logged in, it should not be possible to create a websocket and listen for events from for example http://www.example.com/chat/pubchat?id=some_id.
Is there any way to make this possible?
As far as I know, I cannot use Nginx/Nchan to make database queries to get only logged on users that way. And Laravel itself cannot limit subscribers since that is handled by Nginx/Nchan (but on the other side, can make database queries).
Thanks for any tips!
EDIT: Seems that I might can use the Drizzle module (https://github.com/openresty/drizzle-nginx-module) and use the session ID as identifier. So if that ID is not present in the session table the connection will be blocked.

Laravel implement user friends (chat) presence

I’m having some trouble understanding how to inplement presence channels in a real-time laravel application.
From what i’ve read in the documentation and a lot of other online resources about this, i only need to broadcast on a Presence channel and have clients listen on that particular channel. By the way, I'm using laravel 5.6 and on the front end I use Larvel Echo.
So, my problem is that channel name I need to broadcast to. If it’s something generic like “chat”, ALL the users in my application will broadcast to this channel and users who have no ideea who that user is (not a friend) receive this notification and they have to process this new information. Ofcourse I can choose not to update the UI or just do nothing if the user is not in their friends list but this just seems like a lot of useless procesaing of notifications on the client side. Doesn’t seem like a good ideea in my opinion.
Second option would be to broadcast presence to a unique channel name like “chat-[unique]” where “[unique]” would be something like the logged user’s id/hash but this just means that every client that loggs in the application has to listen for ALL friends notifications, so he has to connect to chat-5426, chat-9482, chat-4847 and so on, for all his friends. Again, this does not seem efficient. But that’s not all. The friends list is paginated so a user, after is logged in, only sees his first 20 friends (unless he scrolls down) and I have no limit on how many friend a user can have - I can implement a limit but still, it would be in the thousands so I don’t think I can get all the users from the DB in one query. I had the ideea of using this last method, to listen to each user's channel on the front end just as they are, paginated. Then, when scrolling and navigation arround, if a new user is visible in the viewport, add it to my friends object (no UI change) and start listening on his presence channel. I can see this method failing pretty easy though.
However I think about this, it always seems like online presence is verry resource consuming and almost not worth it for a small startup, I don't know. I have no ideea what a good way would be to implement it as I`ve never done it before. I would greatly appreciate any help with this because all online resources I've found on the subject implement the first method I asked about, all users connectiong to a generic channel but this always works in tutorials because they only have like 2-3 users in the DB and none mantion a user having friends. I can't see this working in the real world, but I may be wrong.
Thanks in advance

how to redirect other users asp.net mvc 3

I have a problem and i think its have to do with sessions. (at least i want it to be related to sessions because i need to use sessions),
Suppose i have a list of names of people logged in, on my site .
I want that once i press someone's name, that he and I will get a game board. (No confirmation at the moment ..).
Now how do i get the game board , it's obvious. but, how to make that the other user will get one .. ?(the user that i chose from the list).
The other user, he is like a passive user, he suddenly getting a game board.
someone understands?
Thanks in advance .
Seems like you need some COMET/push technology. You may take a look at SignalR which would allow you to push contents to specified clients. Basically all clients will subscribe to notifications from the server and when a particular request comes from an administrator you could then send notification to the selected clients.

How does incoming mail notification on Gmail works?

I'm wondering how it's implemented in Gmail, that every time you receive e-mail, the list of mails is automatically refreshed. It looks like the server is sending some kind of event to the browser, but how is it possible? Or maybe it’s simle: the browser ask the server for new messages every let’s say 2 seconds? But it would probably kill the performance…
Anyone have some ideas?
EDIT: OK, so if it's the simple answer, how do they manage performance? When I send an email from an other account to the gmail account the view is "refreshed" almost instantly. You were saying about a simple function that returns true / false, but it must have some logic (db connection or reads some files). How they manage it?
See also: How is GMail Chat able to make AJAX requests without client interaction?
Dont know exactly which technoloy Gmail uses, but the concept is to open a channel - using reverse AJAX, comet or sprocket based techniques.
Think of it as the client requesting the server for data, but the server does not return for one minute unless it has new mail. Using this technique, the client can almost show the results in a real time manner and it does not have to poll every 2 secs. Makes sense?
gmail is, in fact, polling the server for updates. Not as often as every two seconds, though. That would be madness. A bit of testing with Tamper Data makes it look like maybe every 20 seconds, though there seem to be multiple events going through that confuse it a bit.
Regarding your edit, I imagine they might have a last-activity timestamp on the account tracking in their database, with the client polling query retrieving that via Ajax and comparing with its last sync to determine whether it needs to do a full update.
You have right with simple answer. Google Mail checking new messages on server via AJAX.
It must be some kind of ajax listener that get informations every X seconds.
I already set something like that for one of my projects. What I was doing is calling a function that was returning true or false. True if the page needed to be refreshed, false otherwise. Then if you have an update, you do another call to get the actual update. This way you don't have to refresh everything every time... but it's still intense on the server if you have a lot of users.
In other words and like chaos said, it's polling the server.

Resources