Node.js + codeigniter


Question

Recently been reading up on Node.js and how it is a great webserver and supports sockets even. I was thinking of using it on a project of mine, but i still haven't been able to figure out to to interact from CI to node.js. There was a project done for it with Drupal and it seems to be working well however i still can't figure out how they integrated it together.

Just wondering if anyone has any experience with the idea.

http://drupal.org/project/nodejs

http://www.youtube.com/watch?v=UV8lbdJfESg

Example:

User posts a comment via AJAX Comment gets store in DB All users watching the thread gets notification

now th part where after its submitted the comment and the notification, how does the msg get sent to node.js

1
12
6/28/2013 12:55:33 PM

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.

18
5/23/2017 12:09:20 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon