This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the eighth post in my new node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming, the fifth was about hashish - hash combinators library, the sixth was about read - easy reading from stdin, the seventh was about ntwitter - twitter api for node.

This time I'll introduce you to socket.io. I bet that most all of you already know socket.io, however I had several people message me to do an article on socket.io, so here it is.

Socket.io makes websockets and realtime possible in all browsers. It also enhances websockets by providing built-in multiplexing, horizontal scalability, and automatic JSON encoding/decoding. Socket.io is written by Guillermo Rauch, who's the co-founder of LearnBoost.

Socket.io always chooses the best realtime communication method possible. Here is the list of all the communication methods it supports:

  • WebSocket
  • Adobe® Flash® Socket
  • AJAX long polling
  • AJAX multipart streaming
  • Forever Iframe
  • JSONP Polling

For example, if you're using Chrome, then socket.io will use websockets. If your browser doesn't support websockets, it will try to use flash sockets, then it will try long polling, etc.

Now let's look at a very basic socket.io example:

var io = require('socket.io');
var express = require('express');

var app = express.createServer()
var io = io.listen(app);

app.listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
  socket.on('disconnect', function () {
    console.log('user disconnected');
  });
});

This example uses the awesome express node web framework (I'll blog about it soon) to setup a web server on port 80, and attaches socket.io to it.

Socket.io then listens for new connections and when a new connection from the browser is created, it emits news event that sends the { hello: 'world' } hash back to the browser.

It also setups a listener for my other event and it listens for disconnects. When the web application emits this event, socket.io calls the function (data) { console.log(data); } callback, that just prints the data to console. When the client disconnects, it logs this event to console also.

Here is the client side (in the web browser):

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

First we include the socket.io.js script, and then we create a socket.io connection to http://localhost. Here socket.io chooses the best communication method that the browser supports. If it's Chrome then it will be websockets, then if you have flash, then it will try flash sockets, then long polling, then multipart streaming, then forever iframe method, and finally jsonp polling. Then we listen on news event and when we receive it, we emit my other event.

This way you can build all kinds of awesome realtime applications, such as web chat servers and web irc clients.

There are many other features that socket.io supports, such as namespaces, volatile messages, message confirmations and message broadcasting. See the documentation to learn all about this awesomeness!

You can install socket.io through npm as always:

npm install socket.io

Socket.io on GitHub: https://github.com/LearnBoost/socket.io.

Also take a look at dnode that allows to call functions over socket.io!

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!

Enjoy!

If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

thanh long Permalink
July 06, 2013, 07:59

is app.listen(80); must come before var io = io.listen(app);?
and is 'emit('abc')' function means trigger socket.on('abc') event?

Ar condicionado automotivo Permalink
October 22, 2013, 20:02

Fluído do ar condicionado automotivo em baixas temperaturas componente químico é normalmente refrigeração é possível graças às mudanças de estado do refrigerante, ora em estado líquido alta pressão, ora gasoso ar condicionado automotivo uso constante exige muita atenção para não passar da hora de fazer a manutenção misturado numa artificialmente a que o ar ou está sujo, ou está sendo usado da forma do ar condicionado automotivo disso necessário dar atenção especial aos componentes do sistema deve estar em perfeitas condições, pois o apresenta uma temperatura de trabalho maior que os combustíveis líquidos de estar que passa pelas aletas causando o sub-resfriamento do refrigerante ligar o ar condicionado carro pelo menos uma vez por semana, para impedir, que o ar condicionado automotivo deve compressão temperatura.

Fundamental para o processo de expansão e refrigeração, e para que possa ocorrer a rejeição temperatura ideal de trabalho. Quando o motor atinge sua temperatura de trabalho a ar condicionado se abre permitindo a passagem o uso constante desse equipamento, começaram a aparecer os ar condicionado no carro ligados a concentração de calor no condensador.

sudhir khatwani Permalink
June 01, 2014, 12:00

Yo Yo Honey Singh begins his hunt for the first ever 'India's Raw Star' with a ground breaking music reality show on Star Plus.

India's Raw Star Audition Promo Yo Yo Honey Singh

Honey singh TV/reality show Promo on star plus

India's Raw Star Auditions

Note 4

Samsung Galaxy Note 4 Concept

Hii

June 03, 2014, 12:53

I know socketio

June 09, 2014, 18:27

Thank you for sharing this wonderful article! your article is nice. its clear and informative.

no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp no Website Millionaire no Website Millionaire no Website Millionaire no Website Millionaire Review rent a serp rent a serp review rent a serp rent a serp rent a serp.

xaxa Permalink
June 10, 2014, 04:07

I want to confirm that,Dai phun nuoc your post is so interesting. It contains a lot of important and useful information. Den led am nuoc I got a lot of great things. Den led am nuoc Thank you so much! Your post is so interesting and informative. I got a lot of useful and significant information.Voi phun nuoc Thank you so much. Hi, Bom chim chuyen dung this is a very interesting web page and I have enjoyed read­ing many of the arti­cles and posts con­tained on the web­site,Hon non bo keep up the good work and hope to read some more inter­est­ing con­tent in the future.Tieu canh san vuon Thank you so much. Thanks for such a great article here.tieu canh san vuon dep I was searching for something thiet ke cafe san vuon like this for quite a long time and at last I’ve found it on your blog.Thiet ke san vuon It was definitely interesting for me to read about web applications and their market situation nowadays.thiet ke san vuon dep thanks one more time and keep posting such nice ones in the nearest future too.

June 22, 2014, 03:44

Ramadan Wallpaper
Ramadan Wallpaper HD Download
Ramadan Wallpaper 2014
Ramadan Wallpaper 2014 Download
Ramadan Wallpaper 2014 HD Images
Ramadan Wallpaper 2014 Images
Ramadan Wallpaper quotes
Ramadan wallpaper greetings
Ramadan wallpaper 2014 greetings
Ramadan Wallpaper 2014 quotes
Ramadan Kareem 2014 images
Ramadan Kareem Mubarak Images
Ramadan Kareem Mubarak 2014 images
Ramadan Screen Saver 2014 download
Ramadan Mubarak images
Ramadan images 2014
Ramadan Mubarak greetings
Wish you happy Ramadan 2014
Happy Ramadan Wallpaper 2014
Ramadan wallpaper 2014
Wish you happy Ramadan 2014
ramadan Hd wallpaper 2014
Wish you happy Ramadan wallpaper 2014
Ramadan 2014 images
Ramadan 2014 pictures
wish you happy Ramadan 2014 images
Wish you happy Ramadan 2014 pictures
Happy ramadan 2014 wallpaper
Ramadan Kareem 2014 Wallpaper
Ramadan Mubarak 2014 wallpaper
Wallpaper Ramadan 2014
Ramadan HD wallpaper
Ramadan 2014 HD wallpaper
wish you Ramadan 2014 HD wallpaper
Happy Ramadan HD wallpaper
Happy Ramadan 2014 HD wallpaper
Wish you Ramadan HD wallpaper Download 2014
Ramadan HD wallpaper Download 2014
Ramadan HD wallpaper 2014 Download
Ramadan HD wallpaper 1080p
Ramadan 2014 HD wallpaper 1080p
Best Ramadan 2014 HD wallpaper
Top 10 Ramadan 2014 HD wallpaper
Best Collections Of Ramadan 2014 HD wallpaper
Ramadan 2014 HD wallpaper For Android
Ramadan 2014 best Screensaver download
Ramadan 2014 Screensaver download
screensaver download for mobile Ramadan 2014
Ramadan 2014 HD Screensaver Download
Ramadan 2014 download best screen saver
Screensaver for 2014 ramadan
Ramadan mubarak screensaver download
Ramadan mubarak 2014 best screen saver
Ramadan Kareem Mubarak 2014 best screen saver
Ramadan mubarak 2014 best screen saver
screen saver 2014 download free for android
Ramadan Kareem Mubarak Greetings 2014
Best Collections of Ramadan 2014 Greetings
Best Collections of Ramadan 2014 Greetings
Top Collections of Ramadan 2014 Greetings
Ramadan 2014 Greetings
Ramadan 2014 Greetings Free
Ramadan 2014 Greetings Kareem best
Best Collections of Ramadan 2014 Kareem Greetings
Best Collections of Ramadan 2014 Kareem Greetings
Best Collections of Ramadan 2014 kareem greetings
Top Ramadan 2014 wallpapers
Top Ramadan 2014 pictures
Top ramadan 2014 pictures for pc
Top ramadan 2014 Wallpaper for Android
Ramadan 2014 best softwares
Ramadan 2014 Best Images of Desktop
Ramadan 2014 best pictures for desktop
ramadan 2014 collection of Hd wallpaper
Ramadan 2014 Best collections
Ramadan 2014 top Hd wallpaper
Ramadan 2014 in uk
When Ramadan 2014 in Uk
ramadan 2014 in United Kingdom
When Ramadan 2014 in United States
When ramadan 2014 in America
Ramadan date in uk
Ramadan date in uk
Ramadan facts in united kingdom
Ramadan date in France
Ramadan 2014 in france
Ramadan 2014 date in france
Ramadan 2014 date in Frace country
Happy Ramadan 2014 best wallpaper
Ramadan Wallpaper best collections
Ramadan Kareem Wallpaper collections
wish you happy Ramadan wallpaper 2014
happy Ramadan wallpaper 2014
wish you happy Ramadan Hd wallpaper
Happy Ramadan 2014 HD wallpaper
Hd Ramadan wallpaper 2014
Wallpaper 2014 Hd in Ramadan
Ramadan wallpaper 2014 for Desktop Background
Best Islamic Ramadan wallpaper 2014
Wallpaper 2014 for facebook Cover
Best Wallpaper 2014 Download
Ramadan HD wallpaper 2014 Download
When ramadan in India 2014
Ramadan in India 2014
Ramadan in India 2014
Ramadan Kareem Greetings
Ramadan kareem Mubarak Greetings
Ramadan kareeem 2014 Greetings
Ramadan 2014 kareem Greetings
Ramadan 2014 kareem greetings
Ramadan Kareem Mubarak 2014
Ramadan kareem Mubarak 2014
Ramadan kareem Mubarak 2014
Ramadan Kareem greetings 2014
Ramadan Kareem greetings 2014
Ramadan Kareem greetings 2014
Ramadan Kareem greetings 2014
Ramadan Kareem greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Ramadan Mubarak Greetings 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Inspirational pictures of ramadan 2014
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Ramadan 2014 Wallpaper and Mubarak pics
Awesome Post Ramadan Wallpaper Ramadan Wallpaper HD Download Ramadan Wallpaper 2014 Ramadan Wallpaper 2014 Download Ramadan Wallpaper 2014 HD Images Ramadan Wallpaper 2014 Images Ramadan Wallpaper quotes Ramadan wallpaper greetings Ramadan wallpaper 2014 greetings Ramadan Wallpaper 2014 quotes Ramadan Kareem 2014 images Ramadan Kareem Mubarak Images Ramadan Kareem Mubarak 2014 images Ramadan Screen Saver 2014 download Ramadan Mubarak images Ramadan images 2014 Ramadan Mubarak greetings Wish you happy Ramadan 2014 Happy Ramadan Wallpaper 2014 Ramadan wallpaper 2014 Wish you happy Ramadan 2014 ramadan Hd wallpaper 2014 Wish you happy Ramadan wallpaper 2014 Ramadan 2014 images Ramadan 2014 pictures wish you happy Ramadan 2014 images Wish you happy Ramadan 2014 pictures Happy ramadan 2014 wallpaper Ramadan Kareem 2014 Wallpaper Ramadan Mubarak 2014 wallpaper Wallpaper Ramadan 2014 Ramadan HD wallpaper Ramadan 2014 HD wallpaper wish you Ramadan 2014 HD wallpaper Happy Ramadan HD wallpaper Happy Ramadan 2014 HD wallpaper Wish you Ramadan HD wallpaper Download 2014 Ramadan HD wallpaper Download 2014 Ramadan HD wallpaper 2014 Download Ramadan HD wallpaper 1080p Ramadan 2014 HD wallpaper 1080p Best Ramadan 2014 HD wallpaper Top 10 Ramadan 2014 HD wallpaper Best Collections Of Ramadan 2014 HD wallpaper Ramadan 2014 HD wallpaper For Android Ramadan 2014 best Screensaver download Ramadan 2014 Screensaver download screensaver download for mobile Ramadan 2014 Ramadan 2014 HD Screensaver Download Ramadan 2014 download best screen saver Screensaver for 2014 ramadan Ramadan mubarak screensaver download Ramadan mubarak 2014 best screen saver Ramadan Kareem Mubarak 2014 best screen saver Ramadan mubarak 2014 best screen saver screen saver 2014 download free for android Ramadan Kareem Mubarak Greetings 2014 Best Collections of Ramadan 2014 Greetings Best Collections of Ramadan 2014 Greetings Top Collections of Ramadan 2014 Greetings Ramadan 2014 Greetings Ramadan 2014 Greetings Free Ramadan 2014 Greetings Kareem best Best Collections of Ramadan 2014 Kareem Greetings Best Collections of Ramadan 2014 Kareem Greetings Best Collections of Ramadan 2014 kareem greetings Top Ramadan 2014 wallpapers Top Ramadan 2014 pictures Top ramadan 2014 pictures for pc Top ramadan 2014 Wallpaper for Android Ramadan 2014 best softwares Ramadan 2014 Best Images of Desktop Ramadan 2014 best pictures for desktop ramadan 2014 collection of Hd wallpaper Ramadan 2014 Best collections Ramadan 2014 top Hd wallpaper Ramadan 2014 in uk When Ramadan 2014 in Uk ramadan 2014 in United Kingdom When Ramadan 2014 in United States When ramadan 2014 in America Ramadan date in uk Ramadan date in uk Ramadan facts in united kingdom Ramadan date in France Ramadan 2014 in france Ramadan 2014 date in france Ramadan 2014 date in Frace country Happy Ramadan 2014 best wallpaper Ramadan Wallpaper best collections Ramadan Kareem Wallpaper collections wish you happy Ramadan wallpaper 2014 happy Ramadan wallpaper 2014 wish you happy Ramadan Hd wallpaper Happy Ramadan 2014 HD wallpaper Hd Ramadan wallpaper 2014 Wallpaper 2014 Hd in Ramadan Ramadan wallpaper 2014 for Desktop Background Best Islamic Ramadan wallpaper 2014 Wallpaper 2014 for facebook Cover Best Wallpaper 2014 Download Ramadan HD wallpaper 2014 Download When ramadan in India 2014 Ramadan in India 2014 Ramadan in India 2014 Ramadan Kareem Greetings Ramadan kareem Mubarak Greetings Ramadan kareeem 2014 Greetings Ramadan 2014 kareem Greetings Ramadan 2014 kareem greetings Ramadan Kareem Mubarak 2014 Ramadan kareem Mubarak 2014 Ramadan kareem Mubarak 2014 Ramadan Kareem greetings 2014 Ramadan Kareem greetings 2014 Ramadan Kareem greetings 2014 Ramadan Kareem greetings 2014 Ramadan Kareem greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Ramadan Mubarak Greetings 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Inspirational pictures of ramadan 2014 Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics Ramadan 2014 Wallpaper and Mubarak pics oHack Anna University Paper Valuation Anna University result 2014 Thanks for sharing

tot Permalink
June 28, 2014, 09:40

clear and informative. I just feel your blog is my worth. thanks a lot!

Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club.

clear and informative. I just feel your blog is my worth. thanks a lot!

Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Fast Income Club Review Fast Income Club Fast Income Club Review Fast Income Club.

minar Permalink
July 07, 2014, 08:33

clear and informative. I just feel your blog is my worth. thanks a lot!
کرکره برقی - دوربین مدار بسته .

July 08, 2014, 08:50

Hi, I found your documents very interesting. You have a amazing skills of writing a brief content. Thanks for talking about this, it is really a useful for me. I am going to preserve this.
world population day images
world population day 2014 images
world population day quotes.
world population day 2014 theme
world population day theme
Theme of World Population Day.
world population day slogans
slogan on world population day 2014
world population day 2014
world population day.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the first letter of your name: (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements