Integrate node js and socket IO with codeigniter Integrate node js and socket IO with codeigniter codeigniter codeigniter

Integrate node js and socket IO with codeigniter


I think you're misunderstanding how the socket.io works. You would never listen to your CI view. You would always be sending messages to (and receiving messages from) the NodeJS server on port 8000. Codeigniter's views are simply static, and there is no reason to "listen" to it since it will only load once.

The key point from that answer you referenced:

users will use codeigniter URL and when open the page, i have this script on my CI view page that connects to my Nodejs app

Therefore, you load your browser with the CI view, then listen for events from the NodeJS server through the JavaScript in your CI view.

You can then also push events to the NodeJS server from the JavaScript in your CI view.


Use Dnode, it is an asynchronous RPC system for node.js that makes it talk to php (and vice versa) directly (php side you can call on your codeigniter controller)

I wrote a linkedin post recently about this

https://www.linkedin.com/pulse/make-php-nodejs-talk-each-other-serdar-senay

In the tutorial written for dnode by its founder there's some stale code, so use the code sample in my linkedin post, also dumped below (with better formatting than linkedin):

require ('vendor/autoload.php');$loop = new React\EventLoop\StreamSelectLoop();// Connect to DNode server running in port 7070 and call argument with Zing 33$dnode = new DNode\DNode ($loop);$dnode-> connect (7070, function ($remote, $connection) {  // Remote is A That Provides Proxy object Methods us all from the Server   $remote-> zing(33, function ($n) Use ($connection) {    echo "n = {$n}\n";    // Once We Have the Result We Can close the connection    $connection->end();  });});$loop-> Run();


Here's the flow you're going to want to achieve:

1) node.js server setup with socket.io sockets(.on). If you want to have node.js working over socket 80, look into having iptables forward port 80 to port 3000.

2) Add the socket.io client to the code igniter project. You'll be using this to make the initial connection to the node.js/socket.io connection in the CI View.

3) Setup the different events in the View, to trigger the emits to the server as well as what should happen on receiving a socket message. ie: click a button to add an item to the page, it would emit to the server, and then you might have the client receive a message from the server and update the view so that its current.