Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop a large chat application with Socket.io and Node.js [closed]

I've been working with Socket.io over the past few months, developing a fairly complex chat application with chat rooms, kicking/banning/moderators/friends/etc.

Over the course of development, I've rewritten the app several times and I'm still fighting with my code.

I really like JavaScript, but I find it really hard to maintain the application as it grows. I've read through huge amounts of "tutorials" on how to write chat applications, but they all cover just the most basic aspects. Same goes for all example apps on GitHub, and even most chat applications I found on the web (most of them are just simple IM without any user management).

Some use cases just seem too ridiculous to me, for example kicking a user from a room.

  • moderator clicks on a kick button -> emit event to the server
  • the server pairs the username with a socket (or just broadcast to all users and filter on the client side) -> emit to him kicked event
  • the user emits a logout event to the server and is also displayed a message that he was kicked (logout is just my implementation of punishment)
  • the user is removed from the chat room's user list -> emit current list of users to all users in the room

This doesn't seem too complex, but when I add all the callbacks that happen on the client side to manage the UI (since I use AngularJS, I use events to communicate between controllers), and also ton of callbacks on the server side, since everything is non-blocking, I find this really hard to test.

There's another problem on the client side, where I have to listen for the socket events in multiple places, so I have to have a kind of singleton global socket object, and hook event listeners in multiple places to it.

Am I doing something wrong, or is this callback hell the result of working with websockets with no way around it?

Are there any ways to make developing applications like this easier? For example alternative technology to Socket.io? So far I only found NowJS, which has last commit 5 months ago, and meteor, which is really cool, but looking at the website it doesn't really seem to be stable.

like image 871
Jakub Arnold Avatar asked Sep 05 '12 15:09

Jakub Arnold


1 Answers

Just use promises my friend. jQuery has them built in, but on the nodejs side you can use the q library. Don't write async code without them! They take a little getting used to, but once you're in the mindset writing async code is a breeze.

There are also libraries like async which give you utility functions around callback code. But don't let the popularity fool you. Promises are sweet.

For async code testing you don't need to go much farther than nodeunit which gives you a simple done() function you can call when your test is done, so it can run as long as you like.

like image 128
Andy Ray Avatar answered Sep 20 '22 15:09

Andy Ray