Real-time apps made cross-browser & easy with a WebSocket-like API
Socket.IO is a Node.JS project that makes WebSockets and realtime possible in all browsers. It also enhances WebSockets by providing built-in multiplexing, horizontal scalability, automatic JSON encoding/decoding, and more.
npm install socket.io
var io = require'socket.io';
Next, attach it to a HTTP/HTTPS server. If you're using the fantastic
var app = expresscreateServerio = iolistenapp;applisten80;iosocketson'connection'socketemit'news' hello: 'world' ;socketon'my other event'console.logdata;;;
Finally, load it from the client side code:
For more thorough examples, look at the
Socket.IO allows you to emit and receive custom events.
disconnect, you can emit custom events:
// note, io.listen(<port>) will create a http server for youvar io = require'socket.io'listen80;iosocketson'connection'iosocketsemit'this' will: 'be received by everyone' ;socketon'private message'console.log'I received a private message by ' from ' saying ' msg;;socketon'disconnect'iosocketsemit'user disconnected';;;
Sometimes it's necessary to store data associated with a client that's necessary for the duration of the session.
var io = require'socket.io'listen80;iosocketson'connection'socketon'set nickname'socketset'nickname' name socketemit'ready'; ;;socketon'msg'socketget'nickname'console.log'Chat message by ' name;;;;
If you have control over all the messages and events emitted for a particular
application, using the default
/ namespace works.
If you want to leverage 3rd-party code, or produce code to share with others,
socket.io provides a way of namespacing a
This has the benefit of
multiplexing a single connection. Instead of
socket.io using two
WebSocket connections, it'll use one.
The following example defines a socket that listens on '/chat' and one for '/news':
var io = require'socket.io'listen80;var chat = ioof'/chat'on'connection'socketemit'a message' that: 'only' '/chat': 'will get' ;chatemit'a message' everyone: 'in' '/chat': 'will get' ;;var news = ioof'/news';on'connection'socketemit'item' news: 'item' ;;
Sometimes certain messages can be dropped. Let's say you have an app that
shows realtime tweets for the keyword
If a certain client is not ready to receive messages (because of network slowness or other issues, or because he's connected through long polling and is in the middle of a request-response cycle), if he doesn't receive ALL the tweets related to bieber your application won't suffer.
In that case, you might want to send those messages as volatile messages.
var io = require'socket.io'listen80;iosocketson'connection'var tweets = setIntervalgetBieberTweetsocketvolatileemit'bieber tweet' tweet;;100;socketon'disconnect'clearIntervaltweets;;;
In the client side, messages are received the same way whether they're volatile or not.
Sometimes, you might want to get a callback when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of
What's more, when you use
.emit, the acknowledgement is done by you, which
means you can also pass data along:
var io = require'socket.io'listen80;iosocketson'connection'socketon'ferret'fn'woot';;;
To broadcast, simply add a
broadcast flag to
send method calls.
Broadcasting means sending a message to everyone else except for the socket
that starts it.
var io = require'socket.io'listen80;iosocketson'connection'socketbroadcastemit'user connected';socketbroadcastjsonsend a: 'message' ;;
Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.
Think of this as built-in channels for sockets. Sockets
rooms in each socket.
var io = require'socket.io'listen80;iosocketson'connection'socketjoin'justin bieber fans';socketbroadcastto'justin bieber fans'emit'new fan';iosocketsin'rammstein fans'emit'new non-fan';;
If you just want the WebSocket semantics, you can do that too.
send and listen on the
var io = require'socket.io'listen80;iosocketson'connection'socketon'message' ;socketon'disconnect' ;;
Configuration in socket.io is TJ-style:
var io = require'socket.io'listen80;ioconfigureioset'transports' 'websocket' 'flashsocket' 'xhr-polling';;ioconfigure'development'ioset'transports' 'websocket' 'xhr-polling';ioenable'log';;
(The MIT License)
Copyright (c) 2011 Guillermo Rauch <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.