17

How can I integrate node.js and socket IO in code igniter.

 <script>

        // create a new websocket
        var socket = io.connect('http://localhost:8000');
        // on message received we print all the data inside the #container div
        socket.on('notification', function (data) {
        var usersList = "<dl>";
        $.each(data.users,function(index,user){
            usersList += "<dt>" + user.user_name + "</dt>\n" +
                         "<dd>" + user.user_description + "\n" +
                            "<figure> <img class='img-polaroid' width='50px' src='" + user.user_img + "' /></figure>"
                         "</dd>";
        });
        usersList += "</dl>";
        $('#container').html(usersList);

        $('time').html('Last Update:' + data.time);
      });
    </script>

As mentioned in this SO question here. My view file with codeigniter is in localhost/myproject but nodejs listen to port using localhost:8000. So how can I connect socket IO. Like

 var socket = io.connect('http://localhost:8000');
 //here I need to make socket IO listen to localhost/myproject instead of localhost:8000 .

How is this possible?

5
  • the path is ignored by the socket server, unless it uses it for config somehow, which socket.io does not. Commented Apr 9, 2015 at 8:39
  • I cannot understand what you are telling. Can you please elaborate it more clearly. Commented Apr 9, 2015 at 8:43
  • Have a look at this module, https://github.com/nodejitsu/node-http-proxy Commented Apr 9, 2015 at 8:54
  • Have a look at pusher.com it simplifies this sort of thing a lot Commented Apr 14, 2015 at 15:30
  • I already know pusher. But i want to know nodejs to integrate in CI. Commented Apr 15, 2015 at 6:17

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you mean that nodejs don't listen to URL path. Can you give me simple example about how can I connect nodejs and CI. Thanks for response.
NodeJS is listening on port 8000, and Apache is listening on port 80. They don't "cross-communicate" unless it's done somewhere on the server side.
3
+25

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();

4 Comments

Is dnode some library like pusher or its nodejs module.
it is a nodejs module, as well as a php module, and works with many other languages as well, please click to link and have a read, it links to a tutorial to implement a way of nodejs and php talking to each other, both ways.
to elaborate, yes it has an npm module as well as composer in php*
I had not tried it yet. I will let you know after I will work on it today.
2

you can directly link socket.io.js in the codeigniter view.

<script type='text/javascript' src='http://localhost:8000/socket.io/socket.io.js'></script>

then you will be able to make connection to nodejs server from http://localhost/myproject

 var socket = io.connect('http://localhost:8000');

However, This way you will do all your client side code in codeigniter view. If you want to use nodejs template engine to send html pages to browser then you can change your node.js server port to 80.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.