2

I am new to node.js. I am having a few problems in the code i am trying. Take a look at the code:

var http =require('http');
var url = require('url');
var events=require('events');
var e=new events.EventEmitter();

var i=0;
var clientlist=new Array();

function user(nam,channel) {
this.nam = nam;
this.chan=channel;
}

server = http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');

var pathname = url.parse(req.url).pathname;
pathname=pathname.substring(1);
pathnames=pathname.split("&");
var c=new user(pathnames[0],pathnames[1]);
clientlist[i++]=c;

console.log("user "+pathnames[0]+" joined channel "+pathnames[1]);

e.emit('userjoined',clientlist[i-1].nam,clientlist[i-1].chan);

e.on('userjoined',function(n,c) {
res.write("new user joined with name: "+n+" and he joined channel "+c+"\n");
});

});
server.listen(2000);

The problems i am having are:

  1. I dont get a welcome message in browser for this line of code: res.write("welcome"); But,i get the console.log() message below it in the terminal

  2. The userjoined event that i emitted is not caught. but, after i close the server, everything happens at once. I get the welcome message in the browser, and the callback for the userjoined event.

Can someone tell me what is going wrong here? Thanks

5
  • are you calling respond.end and I'm missing it? You have to tell it to write the stream to the browser instead of buffering it. Commented Jul 20, 2011 at 22:12
  • I do not want to call response.end() bcoz that will close off the connection. I want it to be kept alive. anyways, the problem seems to be with node0.5.0 version. The same code works in node0.4.9 version :/ Commented Jul 20, 2011 at 23:32
  • Are you trying to use a socket? You still have to tell the server to send info to the client every so often. A flush at the very least. Commented Jul 20, 2011 at 23:36
  • @jcolebrand: I am not using sockets. What i am trying to do is, keep the connection open and send data to client whenever an event occurs. i looked into socket.io, but for some reason,i couldn't even get the hello world program to work. The socket.io documentation is highly inconsistent. do u think its a better option? Commented Jul 20, 2011 at 23:59
  • Yes, I know it is for what you're describing. Commented Jul 21, 2011 at 0:32

1 Answer 1

2

ok there are several issues:

  1. you need to declare the e.on userjoined before you call it
  2. you need a res.end() in the e.on userjoined.

Here is the code fixed:

var http =require('http');
var url = require('url');
var events=require('events');
var e=new events.EventEmitter();

var i=0;
var clientlist=new Array();

function user(nam,channel) {
this.nam = nam;
this.chan=channel;
}

e.on('userjoined',function(res,n,c) {
console.log("iuser "+pathnames[0]+" joined channel "+pathnames[1]);
res.write("new user joined with name: "+n+" and he joined channel "+c+"\n");
res.end();
});

server = http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');

var pathname = url.parse(req.url).pathname;
pathname=pathname.substring(1);
pathnames=pathname.split("&");

var c=new user(pathnames[0],pathnames[1]);
clientlist[i++]=c;

console.log("user "+pathnames[0]+" joined channel "+pathnames[1]);

e.emit('userjoined',res,clientlist[i-1].nam,clientlist[i-1].chan);

});
server.listen(2000);
Sign up to request clarification or add additional context in comments.

1 Comment

The problem is bcoz if response.end. but, I dont want to call response.end bcoz that will close the connection. This problem occurs in node0.5.0 only ... Not in node0.4.9

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.