Socket.io: 1.0.0 Replacements for socket.set() and socket.get()

Created on 1 Apr 2014  路  4Comments  路  Source: socketio/socket.io

You used to be able to store arbitrary data along with a socket, using socket.get(key, callback) and socket.set(key, value, callback). How can we do this in 1.0.0?

Just setting random values on the socket seems to work, but doesn't sound safe. e.g. socket.foo = 'bar'.

Most helpful comment

Any resolution or best practice for replacing old .set() and .get() -> we should instead use a prop directly on client object? Is there a module ( as mentioned above ) that does this more elegantly?

All 4 comments

It definitely should be safe to store properties on the object. The reason I'm removing it is that I don't want persistence at the socket.io layer. We can build that in other modules.

I'm definitely including a polyfill as part of the migration guide, however.

@guille Is it ready in 0.9.x as well? To make code ready for 1.0 and get rid of those get and set calls adding unnecessary callback nesting? Or object passed as socket can change identity while retaining the same id, client connection and attributes?

I don't see any polyfill in the migration guide. How is this done in 1.0 exactly?

i'm using this as middleware for now:

configurable = require 'configurable'
module.exports = (socket, next) ->
    configurable socket
    next()

Any resolution or best practice for replacing old .set() and .get() -> we should instead use a prop directly on client object? Is there a module ( as mentioned above ) that does this more elegantly?

Was this page helpful?
0 / 5 - 0 ratings