0

I am in the middle of making a social network, and i want it to be as smooth as facebook.
Like if you look in a console and look at logging, it doesn't update all the time with ajax calls.

on my site i have to load: notifications(the number of new notifs and the notifs themselves), friend requests(same as notifications), online friends(if there are any online it will load the pictures of the online users.) thats 6 ajax calls that is loaded every 10 second. and this causes a huge bandwidth waste and server requests.

Therefore i thought, what if the SERVER told the CLIENT when there was a new update instead of the CLIENT asking the SERVER every 10 seconds.

i have googled this problem and read about ajax push, and a framework called comet. i just can't seem to find any info on how to implement this on jQuery.

TimWolla
  • 28,958
  • 8
  • 59
  • 84
Flaashing
  • 641
  • 2
  • 10
  • 18
  • Not trying to be a jerk, but trying to build the next Facebook without a basic understanding of the web's client/server protocols is going to be a bit of a daunting task. Anyways, jQuery is client side javascript, so in no way can 'push from the server'. However, you may want to take a look at "HTML5 Notifications" – DA. Jan 27 '12 at 21:03
  • Have you thought about implementing a cron job that will do a server wide push every x amount of time? It will load a file that will run all on it's own however often you specify. I use timers for my energy bars that work on the server time but update them thru a cron job every five minutes. – HenryGuy Jan 27 '12 at 21:04
  • Even if the server could initiate a request to the client, the nature of communication in computing is such that there would _need_ to be a listening thread periodically checking for a received message. – calebds Jan 27 '12 at 21:05
  • i am not trying to build the next Facebook, not at all. What i am trying is to make a little simple social network for me and my friends, but at the same time i want to get more experienced in developing. Thats why. besides i have a shitty webhost their servers cant handle 1000 users on a PHP website, whit my website and all the ajax calls, that would get me suspended. – Flaashing Jan 27 '12 at 21:06

3 Answers3

1

I looked briefly into Comet. It appears to be ambitious, experimental and won't run on just any old server.

As I understand it, Comet doesn't really push as such but does something called "long polling", which I won't try to describe here. The web already has several good texts on the subject.

Personally, I would stick with the current plan (conventional AJAX) but make one general purpose call with all the necessary data bundled into an object and JSON encoded. This will reduce 6 requests down to one (every 10 seconds).

You can box-clever by returning nulls within the returned object for information that hasn't changed thereby minimising the length of each response.

Beetroot-Beetroot
  • 17,664
  • 3
  • 35
  • 44
  • yes i see what you mean. I don't have access to the server and this comet is way out of my doing. I will try to make 1 call but im not sure how to work it out; i make 1 call which will return 6 yes or no's if theres a yes i make new call corresponding to the yes? or if theres a yes the server gives the data back and the ajax call could contain 6 results ? which do you prefer ? – Flaashing Jan 27 '12 at 21:19
  • Okay, but how do i split the string and but the data in the right variables ? – Flaashing Jan 27 '12 at 21:24
  • jQuery makes life easy in this area. Make a .getJSON() request (or its .ajax() equivalent). A json-encoded response (simple in php) will be decoded by jQuery to mimic in javascript the array/object that was encoded server-side. Then, you simply probe the response with eg. response.factor1, response.factor2, response.factor3 etc. Each factor can itself be an array or object thus providing as complete a data tree as you require. – Beetroot-Beetroot Jan 27 '12 at 21:30
0

As far as I know, you must make significant modifications on your webserver to get this thing to work. Also, server side php is not really a good option.

Somebody had already asked something similar here: Using comet with PHP?

Community
  • 1
  • 1
Ernest Marcinko
  • 415
  • 3
  • 12
0

You can try socket.io on node.js too. It works great for real time communication http://socket.io/

Ricardo Bin
  • 897
  • 6
  • 12