Skip to main content

Socket.io Join Leave Rooms And Check socket is connected with Room

when we use Socket, we need Know About join & Leave Room.


 io.on('connection', function(socket){  
  socket.join('game');  
 });  

Here socket join "game" room.

"game" is room name. you may use any number as room name.

Let, Send Some Message to Players which are connected with "game" room.


 // sending to all clients in 'game' room except sender  
 socket.to('game').emit('niceGame', "let's play a game");  


Here "niceGame" is event Name.

now we Print All Room which are connected with socket.

Print all rooms which are connected With this.socket


 console.log(socket.adapter.rooms);  


whenever you want to check your socket is connected with Room.


 if(self.socket.adapter.rooms['game']){  
  console.log("you are in room");  
 }  

Thanks.

Comments

  1. Power BI Classes Online
    Discover how to clean and transform data, create interactive dashboards, and write advanced DAX queries for powerful analytics. Build enterprise-ready reporting solutions using Power BI Service and scheduled refreshes.

    ReplyDelete

Post a Comment

Popular posts from this blog

NodeJs - Socket.IO Set Ping Timeout & Ping Interval | node-js-tutorial.blogspot.in

How to Socket.io Specifying Heartbeat Here we Learn Method of Set Ping Timeout & ping interval. There Are Several Way to Set Ping interval & Ping Time out. Please Remember Some note : ping interval is alway small then ping Timeout. Always Set Same ping timeout & ping Interval for both side. (Client side  & Server Side) When You Develop any Client Server Application. Following Are some Pattern Which most Case Peopele use. 1) Type One  var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http, { 'pingInterval': 2000, 'pingTimeout': 5000 }); 2) Type Two For package dependency versions: "express": "^4.12.3", "socket.io": "1.0" ex : var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.set('heartbeat timeout...

How to Use MongoDb Nativ Query With Model in NodeJS | node-js-tutorial.blogspot.com

How to Use MongoDb Nativ Query With Model in NodeJS | node-js-tutorial.blogspot.com In this artical, you have learn how to use mongo native query with node model. let understand some code. Following code use for conver Sting To Object ID. Declare in top of Your code. var ObjectId = require('mongodb').ObjectID; Nativ query Understand Object Id not String. let userId = '123232323232'; // ID of User. Your_Model.native(function (err, modelObj) { modelObj.update({ "_id": new ObjectId(userId) }, { $push: { <feild>: { <feild>: <value> } } }, function (err, res) { if (err) { // Print Error Log console.log(err); } else { // Print Success Log console.log(res.result); } }); }); // End Nativ Here Que...

How To Conver Normal ID to ObjectID in NodeJS MongoDB | node-js-tutorial.blogspot.com

In this artical, you have learn how to Conver String to Object ID. It is use when you run Nativ query thne you need to convert your id to objectId. let understand some code. Following code use for conver Sting To Object ID. var ObjectId = require('mongodb').ObjectID; // Your code Here. let id = '123455667dwdsedr09e9fr0fd'; // Now conver this id to Object ID. let ObjID = new ObjectId(id); Here, Now You Use ObjID in Any Native Query. Thanks.