-
Notifications
You must be signed in to change notification settings - Fork 0
Migrating 0.6 to 0.7+
This document is a starter guide for migrating Socket.IO v0.6.x applications to Socket.IO v0.7.x.
In v0.6.x you would instantiate a socket and connect to it with something like:
var socket = new io.Socket(host, options);
socket.connect();
In v0.7.x you should use io.connect
, without new
.
var socket = io.connect(uri, options);
In addition, you no longer need to pass port
, host
, secure
. You simply pass a URL:
// connect at the same host / port as your website
var socket = io.connect();
// different port or host
var socket = io.connect('http://url.com');
// secure
var socket = io.connect('https://localhost');
You no longer need to set WEB_SOCKET_SWF_LOCATION
global. It's always automatically loaded, as long as socket.io.js
is being loaded correctly. You can still set it if you want to serve the .swf
file from a different location.
Some configuration options changed names. Configuration options are passed as the second argument to io.connect
.
connectTimeout | 'connect timeout' |
tryMultipleTransports | 'try multiple transports' |
reconnectionDelay | 'reconnection delay' |
maxReconnectionAttempts | 'max reconnection attempts' |
For example:
io.connect('http://localhost', { 'connect timeout': 5000 });
To listen on events, you now listen on io.sockets
instead of io
:
var io = require('socket.io').listen(app)
io.sockets.on('connection', function (socket) { });
We made this change because socket.io now supports multiple sockets (multiplexing) over the same connection:
io.of('/chat').on('connection', function (sockets) { });
io.of('/news').on('connection', function (sockets) { });
More information on the above can be found here.
In v0.6, socket.send
would automatically convert an object like { a: 'b' }
to JSON. You would send data to a client with:
socket.send({ a: 'b' });
While this is cool, it poses a fundamental problem. JSON not only encodes objects, but also strings, numbers, etc! So, the API is more clear if you explicitly state you want to pass JSON (since there's a performance penalty associated with encoding/decoding JSON). In v0.7, use the json
flag:
socket.json.send(data[, callback]);
Now you can also emit and receive custom events between the browser and server.
socket.emit('custom event'[, arguments][, callback]);
socket.on('custom event', callback);
Arguments for events get encoded in JSON automatically for you. More information on the above can be found here.
In v0.7.0, if you want to send a message from a particular socket to the rest of the sockets, you can use the broadcast
flag:
socket.broadcast.send('a message');
socket.broadcast.emit('a event'[, arguments]);
If you want to send a message to everyone you can reference io.sockets
:
io.sockets.send('message');
io.sockets.emit('event');
If you made use of the sessionId
property of socket in v0.6, this is now simply .id
.
// v0.6.x
var sid = socket.sessionId;
// v0.7.x
var sid = socket.id;
// v0.6.x
socket.clients[sid].send({ a: 'b' });
// v0.7.x
io.sockets.sockets[sid].json.send({ a: 'b' });
io.sockets.socket(< session id>).send('my message')
io.sockets.socket(< session id>).emit('event name'[, arguments])