14

I am a PHP developer and the title basically says it all. However I was hoping on some more in-depth information as I am starting to get confused about how the flow for the project I work on should go.

For an (web) application I need to implement a feature like Facebook does it with notifying users about replies/comments and instantly showing these.

I figured I could use long-polling with ajax requests but this does not seem to be a nice solution as the notifications never really are instant and it is resource heavy.

So I should use some form of sockets if I understand correctly, and Node.Js would be a good choice. So based on the last assumption I now get confused about the work flow.

I thought about two possible solutions:

1) It seems to me, that if I would use Node.Js I could skip using PHP at all and base the application on Node.js only.

2) Or I could use PHP as a base and only use Node.js for notifying users and instantly showing messages but saving the data using PHP and Mysql.

These two possibilities confuse me and I can't make up my mind about what would be the "best" and cleanest way.

I do not have much experience in Node.js, played with it for a while. But managing and saving data seems to be hard in Node.js so that is why I came up with option 2.

I know Facebook is build on PHP so I am assuming that they save the data via PHP and notify / instantly show replies and comments via Node.

Could someone help me out on this?

Thanks in advance!

EDIT: I just noticed, Stackoverflow does something similar. I get a notification in the upper left, and below my question a box with "new answer to this question". I am really interested in the technologie(s) used.

Vincent Cohen
  • 878
  • 7
  • 28
  • 1
    Perhaps you could create a new question for the technologies used by stackoverflow for the notification system or better search the a little bit more in here :) – x_maras Jan 30 '13 at 10:54

3 Answers3

13

Well you could use node.js for the notifications and PHP for your app. By googling I found this about real-time-notifications. You could also just use node.js with socket.io, but this means that you have to learn new technologies as you mention that you have no experience with node.

I haven't used it but you could check this project, for websockets in PHP.

When you have an update that you want to notify users you can use the publish subscriber pattern to notify the intrested in this update. Take a look in Gearman too.

Personally, I've built a notification system using the pubsub mechanism of redis, with node.js+socket.io. Everytime that there is an update on a record then there is a publish on the appropriate channel. If the channel has listeners then they will be notified. I also store the last 20 notifications in a Redis list.

The appplication is built in PHP. The notification system is built in node.js. They are different applications that see the same data. The communication occurs via redis. For example in the Facebook context: 1) A user updates his status. 2) PHP stores this to the database and Redis 3) Redis knows that this update must publish to the status channel of the specific user and it does. 4) All the friends of the specific user are listening to his status channel (here comes node.js) 5) Node.js pushes the notification in the browser with socket.io

As for facebook, I have read in an article that is using long polling for supporting older browsers. Not sure for this though, needs citation...

x_maras
  • 2,177
  • 1
  • 25
  • 34
  • for the "pusub mechanism" you build, did you use any other languages besides node? I am somewhat sceptic about websockets in PHP as I believe PHP itself is not meant to be used in such a way. – Vincent Cohen Jan 30 '13 at 11:03
  • I played with node now after my original question, as I seem to understand Node runs on its own port so if i'd go to for e.g myserver.com:3737 Node runs, but I have PHP running on myserver.com (port 80). But how would it be possible to have my PHP application communicate with Node? Might need to create a new question, but I hoped the answer would be very simple.. – Vincent Cohen Feb 02 '13 at 11:06
  • 1
    Take a look at [haproxy http://haproxy.1wt.eu/] and acls. Shortly what you could do is serve specific requests from your application with node.js and other with other with php. I also think that this should be new question. – x_maras Feb 02 '13 at 16:33
  • Hi, I'm really interestet in what did you ask @VincentCohen "But how would it be possible to have my PHP application communicate with Node?" Did you make a new question ? if so, could you link it please. Thanks in advance – Mollo Jul 04 '13 at 16:29
  • PHP and node do not talk to each other. They both read/write information from/to Redis (you could also use something else, it's not a requirement). Also in my answer that I mention Gearman. You can also check php-resque for background jobs – x_maras Jul 05 '13 at 08:17
  • @user2088447 In the end I did not have the time to complete this. It is still on my todo list. After much reading I came to the understanding that I should use Reddis, like x_maras suggests. I do not fully understand it, but it seems that Node.JS polls the reddis database and it notices changes basically much like javascript events work. – Vincent Cohen Jul 05 '13 at 10:32
  • Alright, so, is not that easy as I thought. Let me see if I got it right -- In order to can stablish a communication between php and node.js is required another tool as intermediary in this case "Gearman" or any other – Mollo Jul 05 '13 at 17:03
  • 1
    No, I think you got it wrong. The "tool" that you mentioned is actually the storage. PHP stores data in Redis (it can be another one as well) and node.js can access this data. The benefit of using Redis as storage in this case is that you can say to the node listener, "Hey, I've got some data that you might be interested in". The node, know and can fetch them "transparently". – x_maras Jul 07 '13 at 17:42
1

AFAIK It would be via two simple methods :

  1. First one that could be very simple is adding a Boolean column to each record that determines if it has been notified or not.

  2. The second method is creating a table to insert all notifications.

However, I'm not sure if there are alternative methods for better performance, But first method is what I do commonly myself. But I think Facebook is using 2nd method, because it has to notify each one to a lot of users.

Your question maybe dublicate of:
Facebook like notifications tracking (DB Design)
Database design to store notifications to users

Community
  • 1
  • 1
Omid
  • 4,575
  • 9
  • 43
  • 74
  • I have checked multiple threads yes, but felt I needed more information. As I am not questioning the tracking of notifications / database design rather more on the flow of the application. – Vincent Cohen Jan 30 '13 at 10:55
0

You could use Server Side Events it involves a bit of JavaScript but nothing overly complicated I think.

The main bulk of this method is PHP though, so you would just use the PHP to query your DB for notifications and SSE will push them to the user.

It does have some limitations though, most notably it's not supported by IE (huge surprise) thought i'd mention it anyway to let you know of other possibilities.

Hope this helps

jampez77
  • 5,012
  • 7
  • 32
  • 52