From 131a2befec3c79a1fd9934ed1dfda0ba85cf2c5f Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 10 Jan 2017 17:46:54 +0100 Subject: [PATCH] [docs] Add an example for socket.join() method (#2813) --- docs/API.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/API.md b/docs/API.md index 2d1040c9ae..6ad2b26c56 100644 --- a/docs/API.md +++ b/docs/API.md @@ -422,6 +422,15 @@ socket.on('news', function (data) { Adds the client to the `room`, and fires optionally a callback with `err` signature (if any). +```js +io.on('connection', function(socket){ + socket.join('room 237', function(){ + console.log(socket.rooms); // [ , 'room 237' ] + io.to('room 237', 'a new user has joined the room'); // broadcast to everyone in the room + }); +}); +``` + The mechanics of joining rooms are handled by the `Adapter` that has been configured (see `Server#adapter` above), defaulting to [socket.io-adapter](https://github.com/socketio/socket.io-adapter). For your convenience, each socket automatically joins a room identified by this id (see `Socket#id`). This makes it easy to broadcast messages to other sockets: