Node.js + codeigniter Node.js + codeigniter codeigniter codeigniter

Node.js + codeigniter


node.js is a non-blocking IO library capable of being used as a Web Server.

Code Igniter is a PHP framework.

Do you want to run a node.js Web Server beside your PHP Web Server and have them talk to each other?

I'd recommend you do one or the other. Re write your entire website in express and now.

If they must talk to each other you can easily open a TCP socket in node by using net.

var net = require('net');var server = net.createServer(function (socket) {  socket.write("Echo server\r\n");  socket.pipe(socket);})server.listen(8124, "127.0.0.1");

Then just use fsockopen in PHP to connect to node over a TCP socket.

Edit:

The live comments is completely independant of CI. You just need to have some socket.io javascript on you CI server pages. Your pages talk to node.js over a seperate socket and never touch the PHP back end. Your socket.io will push data to all your clients and the pages will render new messages with javascript.

All codeigniter needs to do is insert

<script src="url/socket-io.js" /><script src="url/myChat.js" />

Further Edit:

So you need your user to log in over your websocket. I'm not sure how they log in now but sending the same username/password hash to node.js shouldn't be too hard. Get node.js to open a connection to your database where you store users. Then store which channels / threads / chat rooms / messages a particular user is "subscriped" to in a database.

Then when node receives a message from a "channel" it just asks the database which users to push that message to, and then it pushes it.

I answered a similar question about writing a chat server using node and the video tutorial of now has a good example. You should be able to turn "multiple rooms chatting" into "multiple thread commenting" pretty easily.

Further Further Edit

Don't post to the URL comment/add/ when you click add. Don't use ajax. Instead use socket.io.

So something like:

// on the client side$("#add").click(function() {    socket.send("add" + user.toJSON());});socket.on("message", function(m) {    if (/^new/.test(m)) {         var post = m.substring(3);         $("#comments").append($("<div></div>").text(post));    }});// on the server sidevar socket = io.listen(server); socket.on('connection', function(client){     // new client is here!     client.on('message', function(m){         if (/^add/.test(m)) {             client.broadcast("new"+m.substring(3));        }    }); }); 

So simply the client sends a "add comment" message when you click add. The server listens for the add message and broadcasts the message to all other clients. These clients are already listening for the new message, and new appends a comment.


The way Drupal has done it, is use the Node.js plugin Socket.io.When a comment gets posted, drupal notifies the socket with the comment details, node.js then notifies the other "clients".


I think in a general case of using node.js with CodeIgniter, there would be no direct communication between the two.

In the example you described above this could be accomplished using a technique call "long polling" with node.js . (http://blog.nemikor.com/2010/05/21/long-polling-in-nodejs/)

Essentially, your client side AJAX would make a request to your node.js server. This request on the node.js server would start a process that checks your DB for new comments every second (or 5 seconds, etc). When it finds a new comment, it would return it as the response to the client side JS AJAX call, and your JS would handle it from there.

This is one approach in which node.js and CodeIgniter could be combined.

NOTE: Node.js is very good for long polling because you can maintain many simultaneousness requests from one node.js server, due to the non-blocking, event loop based design of node.js