Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible integrate Nodejs with Cakephp?

I want to monitor in real time the data that users enter in comments table. I have an Apache server running, and suppose that has a node server on port 1337.

How would I do that every time someone save new data, eg return me the total number of table rows in comment and show it in a view?

Maybe way is to make the $this->Comment->save($this->request->data); using a different port using Httpsockect?

like image 300
Igor Martins Avatar asked Mar 13 '14 14:03

Igor Martins


1 Answers

Yes, it is possible.

You have multiple ways of solving this, let me give you my ideas

You can simply use long-polling and don't use Node.js at all. It's a suitable solution if there won't be too much traffic there, otherwise you will have a bad time.

You can use websockets and don't use Node.js at all. Here you have a basic guide about websockets and PHP. Although, I am almost sure you won't be able to create "rooms", that is, sending notifications for specific comments.

You can also use Ratchet. This is a more sophisticated library to handle websockets and it supports rooms.

Lastly, if you want to full dive in with Node.js and CakePHP, I would suggest start by watching this talk given on Cakefest 2012 which exactly describe your scenario.

After you have watched that, you might want to learn a little about Socket.io. This is a more complex solution, but it's what I have used when integrating CakePHP and Node.js to create real time applications.

The strategy here is to have the users join a room when they visit /article/view/123, let's say the room name is the articleID, then socket.io will be listening for events happening in this room.

You will have a Cakephp method that handles the save. Then, when user submits the form you don't call directly the Cake action, you have socket.io to dispatch an event, then in your event you pass the data to the server (Node.js) and nodejs will call your cakephp function that saves the data. When Nodejs receives confirmation from CakePHP then you broadcast an event (using socket.io), this event will let know all users connected to that room that a comment has been made.

like image 128
Guillermo Mansilla Avatar answered Sep 29 '22 02:09

Guillermo Mansilla