1

I'm wondering the best way to handle push notifications of comments that my site's users will send to each other.

When a new comment is submitted it will obviously be stored in my database. However, i'd like to also store each user's comments in memcache or as session data (whatever is considered best?) such that every time the user visit my site/refreshes the page, they can quickly get their unread notifications and not hit my database.

One way I thought of doing this was upon the user receiving a push notification, to send an AJAX request back to the server to update their memcache/session data.

The problem is what to do if the user isn't logged in or isn't currently using the site. Will the AJAX request still be sent even though the user isn't there to "receive" it? Or will the AJAX request "wait" and happen as soon as the user revisit the site? Is handling notifications in this way even a good idea in the first place?

tim peterson
  • 22,033
  • 50
  • 162
  • 279
  • Look into Redis. http://redis.io/ Havent implemented it myself, but sounds like what you're looking for. – cgatian Jan 21 '13 at 21:41
  • @cgatian, thanks I was just looking at that, though it's not clear why Redis vs. Memcached in this scenario. Also, I'm a one man-operation and it took alot of time to implement what i've currently done, adding Redis to the mix seems daunting, have any sense of its ease of use? – tim peterson Jan 21 '13 at 21:43
  • Check the answer here for what Redis offers over Memcached: http://stackoverflow.com/questions/10558465/memcache-vs-redis – Erik Nedwidek Jan 21 '13 at 21:51
  • thanks @ErikNedwidek, now that i've thought about it more, it doesn't seem like switching technologies really addresses the issue. My question is more about how push notifications are handled if the user isn't there to receive them. Does the push notification just "wait" until the user shows up or is it lost if the user isn't there to receive it? – tim peterson Jan 21 '13 at 22:07
  • See my answer. There is no true way to do push notifications with AJAX. In AJAX they are always client polling. If the user is not on your site, there is no client to do the polling. – Erik Nedwidek Jan 21 '13 at 22:10
  • @ErikNedwidek see my comment below to your answer. I am doing push notifications. The AJAX request I'm speaking of isn't the push notification itself but instead happens **in response to** the push notification being received. – tim peterson Jan 21 '13 at 22:12

1 Answers1

0

You want to keep your PHP sessions small. Stuffing lots of data into the will cause performance degradation. I wouldn't use them for this purpose. The most I would use it for is to store the last time they checked their comments.

You also are not doing push notifications, you are using client polling. You can do push notifications with Websockets, but not AJAX (you can fake it though with long polling). Unless your user is logged in and has a browser open to your site, there won't be any Javascript running to initiate the AJAX call to check for comments.

Websockets or long polling require that you use a secondary server to Apache. They will eat up your available connections in Apache.

I'm assuming that you want the comments to be persistent, so keep in mind that "forgetting" is a feature of Memcached. Redis can be set up to persist data. If you use memcached as a performance boost, you need to decide if data integrity is important (dropping notifications is ok) or if you need to design in some robustness so that they will always get their notification.

Hope this helps.

Erik Nedwidek
  • 5,896
  • 1
  • 23
  • 23
  • Hi Erik, I am doing push notifications with websockets. My question is about how the push notifications are handled by the client if the user isn't there to receive it. The AJAX request i'm speaking of would be sent by the client in response to receiving the push notification. – tim peterson Jan 21 '13 at 22:09
  • With a websocket, when the browser window or tab is closed or navigated in any way, the websocket is closed. Now if they are reloading the current page or going to another page on your site that will reopen the websocket, you can push any new notifications down that pipe. If they are off the site, you won't be able to do a push. – Erik Nedwidek Jan 21 '13 at 22:16