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 side
var 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.