Skip to content

Commit

Permalink
[docs] Add an example for socket.join() method (#2813)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Jan 10, 2017
1 parent 0be865f commit 131a2be
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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); // [ <socket.id>, '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:
Expand Down

0 comments on commit 131a2be

Please sign in to comment.