Is possible integrate Nodejs with Cakephp?

后端 未结 2 830
长发绾君心
长发绾君心 2021-02-09 05:44

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.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-09 06:09

    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.

提交回复
热议问题