开发者

Node.js + codeigniter

开发者 https://www.devze.com 2023-02-24 20:56 出处:网络
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 inter

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 get开发者_运维技巧s 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


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.


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


I have just found this package:

NodeigniterMVC - an MVC framework for node.js inspired by Codeigniter. It allows custom routing, chaining, and partial view rendering; built-in with helpers, libraries, and a CLI. Fully compatible with Bower.

https://www.npmjs.com/package/nodeignitermvc

So from what I understad, it's a package that runs unde NODE.JS if you haven't started you're project and want them both without getting yourself into too much trouble playing with sockets on your server - this looks ideal.

maybe this helps :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号