Re: [nodejs] Simple chat-room example?

214 views
Skip to first unread message

Ryan Schmidt

unread,
Jan 4, 2013, 8:55:55 PM1/4/13
to nod...@googlegroups.com

On Jan 4, 2013, at 17:38, Josh Santangelo wrote:

> I'm trying to build a simple server where multiple clients connect over TCP, and any message from a client is relayed to all the others. This isn't going to be used for a chat room, but it's basically the same idea.
>
> I tried building this with socket.io but got stuck pretty quickly:
>
> var io = require('socket.io').listen(81);
>
> console.log('foo');
> io.sockets.on('connect', function (socket) {
> console.log('connected...');
>
> socket.on('disconnect', function () {
> console.log('disconnected');
> });
> });
>
> I see "foo" trace out, and on the client side it looks like I'm connected, but "connected" and "disconnected" never trace out, so I'm guessing those event handlers aren't working for some reason.
>
> I'm not married to socket.io, and actually using fewer modules would be better, but I'm surprised I couldn't find a code sample for this use case since it sounds like it's a pretty common node.js usage.

I got socket.io working successfully following a few examples. It seems to be well-regarded and is probably a good place to start.

According to the documentation on http://socket.io you want to listen for 'connection' (not 'connect').

Dennis Kane

unread,
Jan 4, 2013, 10:00:07 PM1/4/13
to nod...@googlegroups.com
I've recently been hip deep working on a chatroom on my site @ http://luvluvluv.info.  I wouldn't call it "simple", but I do use socket.io and I can testify it is da bomb.  My site is supposed to be an in-browser OSX kind of thing.  It's all javascript... client and server.  No markup, no css, just one big blob of a  <script> file (well, two if you count <script src="/socket.io/socket.io.js">!)  It's kinda hard to explain... you just need to see it (with a Chrome browser, of course).


On Friday, January 4, 2013 6:38:17 PM UTC-5, Josh Santangelo wrote:
I'm trying to build a simple server where multiple clients connect over TCP, and any message from a client is relayed to all the others. This isn't going to be used for a chat room, but it's basically the same idea.

I tried building this with socket.io but got stuck pretty quickly:

var io = require('socket.io').listen(81);

console.log('foo');
io.sockets.on('connect', function (socket) {
  console.log('connected...');

  socket.on('disconnect', function () {
    console.log('disconnected');
  });
});

I see "foo" trace out, and on the client side it looks like I'm connected, but "connected" and "disconnected" never trace out, so I'm guessing those event handlers aren't working for some reason.

I'm not married to socket.io, and actually using fewer modules would be better, but I'm surprised I couldn't find a code sample for this use case since it sounds like it's a pretty common node.js usage.

Obviously this is my first time using node.js! Any pointers would be very helpful.

thanks,
-josh

Ben Evans

unread,
Jan 4, 2013, 10:27:35 PM1/4/13
to nod...@googlegroups.com
Hi Josh,

If it's any use to you, when i first gave Node a shot I wrote a very basic chat app. Feel free to use it/reference: https://github.com/bencevans/Little-Chat-Script

Features:

* Socket.IO Realtime Messaging
* Connection Indicator

and that's about it but I hope it's of some help.

Good Luck and Happy Hacking!

Josh Santangelo

unread,
Jan 7, 2013, 4:20:25 PM1/7/13
to nod...@googlegroups.com
Thanks for the replies, all. I'm messing around with it more and not having any luck with "connect" or "connection" events. At this point I can only assume that node/socket is doing the right thing and it's my client code not connecting correctly.

I'm having trouble finding any examples of a C# app connecting to node, but surely someone's done it? The code I'm using is very similar to the sample code at the bottom of this MSDN page: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket(v=vs.95).aspx

Josh Santangelo

unread,
Jan 7, 2013, 5:32:13 PM1/7/13
to nod...@googlegroups.com
I tried the very basic sample code from the socket.io home page in a browser and that seemed to work correctly, further confirming my suspicion that it's on the client side.

Josh Santangelo

unread,
Jan 7, 2013, 8:58:04 PM1/7/13
to nod...@googlegroups.com
In case anyone else comes across this thread, the answer was to not use a raw socket, but rather to use a full client. There is a .NET client here: http://socketio4net.codeplex.com/

Brandon Cannaday

unread,
Jan 8, 2013, 10:15:27 AM1/8/13
to nod...@googlegroups.com
Here's a tutorial that we wrote that provides step-by-step instructions on how to create a chat room with socket.io and Node. Might be helpful.

Reply all
Reply to author
Forward
0 new messages