Search code examples
phpjquerynotificationslong-pollingpush-notification

notification and messaging system in javascript and php (without the need of having to install additional software serverside)


i'm running a social network with a messaging and notification feature. each time a user sends a message or places a notification for another user, a row is inserted into a table news_updates with the details about the message or notification and all his friends are inserted into the news_seen table. (once the message is read, or the item related to the notification is opened, seen is set to 1, i'm doing this at the end of my callback function for my ajax request - i'm gathering all the newsitem_ids from all the news items, that are currently open and then i'm doing a big insert with all the newsitem_ids in it).

news_seen:

newsitem_id bigint, 
user_id big int,
seen int DEFAULT '0'

at the moment, i'm running an ajax request every 3 seconds, to check the news_updates JOIN news_seen for news.

this turns out to be a huge server load now that i'm getting more and more users. i've been reading a lot about xmpp and the like and i think a push notification service would be great for my site.

the only thing is, i can't really decide on which way to go, since there are so many options.

also, i thought about creating my own system. i'm planning to do it like this:

  • create an xml file for each user on initial registration (and run a batch for the already registered users)

  • once a user sends out a news update (i have my own php function for writing them into the db), i include a small command to manipulate the xml file for the respective friends

  • instead of doing my 3sec ajax request, i'd establish a long connection to the xml file using jquery stream and in case changes were made since the last request, i'd do my usual ajax request that polls the data from the db.

  • instead of running my check_seen inside the ajax request, i'd insert all the new items into a global array, to be used by an intervaled function that tests if any item in the list is currently being viewed.

do you think this is a good idea?


Solution

  • To be honest I do not think I would implement your specification.

    • For example I would use a lighter data-model then XML. I would use JSON instead.
    • I would avoid touching the DISC(database) as much as possible(slow).
    • Doing two requests instead of one(long-polling/polling). I would try to avoid this.
    • I would probably try to avoid wasting CPU-time by not using interval functions, but only calling function when needed. I would probably use something like REDIS's pubsub.
    • Polling / Long-polling is (most of the times) a bad idea in PHP because of blocking IO. I found an interesting project named REACT which I believe does non-blocking IO(expensive). I have not tested this(the performance) myself, but this could be an option.
    • For XMPP you will have to install additional software. I for instance liked Prosody for it's easy installation/usage. Then you would need to have a look at BOSH. For your bosh client I would use strophe.js or JSJaC.
    • I would probably use something like socket.io, Faye or maybe vertx.io instead, because it would scale a lot better.