I want to know how to share data being set in a socket in one namespace and access it on another namespace?
I'm fully aware that i can attach data to the socket object itself.Problem arise when i attach the data on one namespace and try to access it on another namespace.
Below demonstrate the problem
var io = require( 'socket.io' );
module.exports.init = function( server ) {
io = io.listen( server );
io.of( '/chatSystem' ).on( 'connection', function( socket ) {
/*handling set nickname event*/
socket.on( 'set.name', function( data ) {
/*attach nickname key to this socket*/
socket.nickname = data.name;
socket.broadcast.emit( 'user.entered', data );
});
});
io.of( '/chatUser').on( 'connection', function( socket ) {
/*handling client event socket.send*/
socket.on( 'message', function( data ) {
data = JSON.parse( data );
data.nickname = socket.nickname; // <--- this is undefined
/*send to all connected client( broadcast will not send message to socket that created the message)*/
socket.broadcast.send( JSON.stringify( data ) );
data.type = 'myMessage';
/*manually send back the message to the socket that created the message*/
socket.send( JSON.stringify( data) );
});
});
};
Is there a way to fix this?