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.
Related
I'm working on a chat room program using ASP.NET, I have made a 'user has joined the chatroom' message, but how would I send a message when the user leaves the page? I tried looking through the network logs, but I can't see any extra messages getting sent through the WebSocket.
This will be tough and inaccurate because there is really no guarantee that you will get notified that the user has disconnected. But there is an onDisconnect event that you can listen for, but in all honesty, I would use a disconnect button and use that to do what you need to do, it would be more consistent, as the disconnect is not reliable (at least the last time I used SignalR, which was like version 1.x)
Nevermind, I've found out about window.onbeforeunload using JavaScript. Though it's not 100% consistent, it's more consistent than I expected.
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
I'm working on a real-time chat. I need to change statuses of the room owner and connected users, together with the UI. Since NodeJS/SocketJS/etc don't guarantee message delivery, I switched to pure Ajax for that.
The system works like that:
- User presses a button to change his status
- An Ajax request is being sent to the server, and a status change request is being saved in the queue in DB
- Users send Ajax heartbeats every second. On the server this heartbeat function also processes the queue (when sent by the room owner). Besides it sends the current statuses of users in the room in response every time.
The issue is: there might be temporary internet problems on both sides, which causes all kinds of problems. This happens due to the fact, that the heartbeat Ajax requests are being processed in an arbitrary order on the server, or the responses are being received in a wrong order on the client side. As a result users have wrong data about current statuses and the UI changes are also wrong.
What is the best approach when making a system like that? What am I doing wrong or how can I fix the issues above?
Thank you!
Have a look at Max's blog a Django-Realtime-Chat and how he does it.
For example, when we have new message on http://facebook.com or http://gmail.com, site gives us signal about this, and this happened without refresh page. how it works? I Imagine this so:
some Jhon is registered user on site.
There is javascript code, which every 10 seconds sends AJAX request to messages table, for check: in table, has or not unread messages for john (architecture, how marking ready / unread messages, here not important), if Yes, site give signal to Jhon "You have several new messages".
Tell please, I am closer to the truth or not? If yes, I have also one questions please.
In modern sites we use websockets which enable the server to push information to the browser with less overhead and no delay. That's what's used on SO.
But as some browsers still don't support them, we must sometimes fall back to a pull-loop as the one you describe.
On the server it may be handled with a queue instead of database queries at each browser request. But there are probably many different implementations on this point.
is there any way in MVC to get the System.Net.Mail SendAsync to work, instead of the blocking Send method?
I tried using it but hit the "Page starting an asynchronous operation has to have the Async attribute" error, which I obviously can't resolve (or can I?) because there is no ASPX page with an #Page directive where I could add the Async attribute.
Help is greatly appreciated :(
If you're using a third party service such as gmail to send your email and relying on SendEmail or SendAsync you may want to set up your own SMTP server which will then relay onto gmail. The benefit is that SendEmail will return much faster and you may be able to forget the whole async thing.
That way if gmail rejects you for whatever reason the email will still sit in your local queue.
This is a quick and dirty way to get somewhat reliable email sending setup.
Instructions (not personally tested, but i did this years ago*) or see this for Windows 7
*of course I only remembered i did this after spending a long time converting over to async :-(
It looks like you want Asynchronous support for ASP.NET MVC. See also "Extend ASP.NET MVC for Asynchronous Action".
This SO question is also relevant.
You may want to consider putting the email into a database table and then have a daemon that sends out emails by processing the queue.
a) if youre not sending billions of emails you can have a log of what you sent
b) you can resend if something fails.
depends how important these emails are, but if theyre important from a business standpoint and mustnt get lost then you should use this approach.