Author Topic: How do I make the 'server' notify the web page when to refresh?  (Read 894 times)

0 Members and 1 Guest are viewing this topic.

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
How do I make the 'server' notify the web page when to refresh?
« on: February 10, 2015, 04:41:17 pm »
I am trying to make a facebook like conversation system and I have pretty much succeeded. Now there are a few confusions. When the message is sent from one user's end, ,I can simple use ajax to insert it into database and have the callback function append the last message at the bottom of list of messages that are being displayed.
However, on the receiver's end, I have to display the message that is the most recent in database (sent by the other user). One way I can think of is keep checking the table containing messages for updates after a certain interval of time, and get over it.
But, in a hypothetical situation, in a hypothetical universe, where my site actually has tens of thousands of users, and each of them have like 50 conversations in their inbox. I can only imagine every few seconds the database being queried a million times which will be too much of a burden and will slow down everything. Now, My question is, is there someway that the web server on the database update 'notify' the webpage to refresh instead of the web browser requesting new page after a specific interval. I'm not a professional web developer or something and have tried googling already. The nearest I've got to is this WEbSOcKeTS thingy, which I'm unsure of.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: How do I make the 'server' notify the web page when to refresh?
« Reply #1 on: February 10, 2015, 05:28:43 pm »
There are some ways. You have Websocket, http://socket.io/ there is also a great thing called EventSource which i recently used.
~Factionwars

Offline h3r0

  • NULL
  • Posts: 2
  • Cookies: 1
    • View Profile
Re: How do I make the 'server' notify the web page when to refresh?
« Reply #2 on: February 11, 2015, 03:27:20 am »
Your best bet for something like this is a websocket, NodeJS(could be fun), or an instant messaging framework like AJAXIM (http://ajaxim.com/) or .

Personally if I were to be doing this I would go the nodeJS route, but if you're 100% stuck on PHP and JS there is this tutorial that could help you clean up your code/develop a good system.

http://www.pixel2life.com/forums/index.php?/topic/42552-ajaxphp-instant-messaging-system/

Offline PsychoRebellious

  • Peasant
  • *
  • Posts: 130
  • Cookies: -6
    • View Profile
    • My Rantings
Re: How do I make the 'server' notify the web page when to refresh?
« Reply #3 on: February 11, 2015, 06:12:48 pm »
For the time being I've utilized the setInteval() for this purpose.

Websockets and NodeJS was suggest to me already by a few folks. I'm thinking of giving NodeJS a go. One more thing I was looking at the facebook's approach. Looking at the headers requests, it is evident that they are using the same 'browser requesting updates' instead of server notifying on update. Everytime you click inside the chatbox the POST request is sent. Try it out. Absurd much? Facebook has billions users, millions of them online at a time. Why isn't this approach slowing them down?

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: How do I make the 'server' notify the web page when to refresh?
« Reply #4 on: February 11, 2015, 07:52:10 pm »
Because they have big data-centers, I guess. Plus, each of those requests isn't that big (less than a KB in your example provided above).
« Last Edit: February 11, 2015, 07:53:29 pm by TheWormKill »
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker