kadhiresan k
kadhiresan k

Reputation: 53

How to unsubscribe a socket from a room when socket disconnected from the server

If socket is disconnected from the server, Will all the subscription of that socket will be removed from the chat-room by default(by sails) or we have to remove it manually(by code)

Upvotes: 2

Views: 1005

Answers (1)

Milad Aghamohammadi
Milad Aghamohammadi

Reputation: 1976

Base of socket.io source code this.leaveAll() will run before fire disconnect event. So no need to leave from rooms manually

Socket.prototype.onclose = function(reason){
  if (!this.connected) return this;
  debug('closing socket - reason %s', reason);
  this.emit('disconnecting', reason);
  this.leaveAll();                    //  leave from all rooms
  this.nsp.remove(this);
  this.client.remove(this);
  this.connected = false;
  this.disconnected = true;
  delete this.nsp.connected[this.id];
  this.emit('disconnect', reason);   // disconnect event fire here
};

Upvotes: 4

Related Questions