Observable Rooms

Observable rooms act like regular rooms but provide additional functionality for keeping track of connected members and linking messages to members.

Subscribing

Subscribing to an observable room works like subscribing to a regular room, simply prefix the room name with observable-.

var room = drone.subscribe('observable-room');
room.on('open', function(error) {
  if (error) {
    return console.error(error);
  }
  // Connected to room
});

Events

Members event

Emits an array of members that have joined the room. This event is only triggered once, right after the user has successfully connected to the observable room.

room.on('members', function(members) {
  // List of members as an array
});

Members array will also contain yourself. You can find yourself from the array like so.

var me = members.find(function(member) {
  return member.id === drone.clientId;
});

Member join event

Member join event is emitted when a new member joins the room.

room.on('member_join', function(member) {
  // Member object
});

Member leave event

Member leave event is emitted when a member leaves the room.

room.on('member_leave', function(member) {
  // Member object
});

Data event

Data event works just like in a normal room would but offers a second additional argument that defines which user sent the message. When the member argument is empty the message was sent using the REST API.

room.on('data', function(data, member) {
  // Data sent by member
});

Member object

Key Optional Type Description
id string Unique ID of the member
authData object Data set by the JWT
clientData object Data set by the JS client

Example member object

{
  id: 'jkawkaf:djkdd21',
  authData: { serverId: 312 }, // Undefined if not set
  clientData: { color: 'Orange' } // Undefined if not set
}

Adding custom data to a member

Custom data can be added to a member using the JavaScript API or JWT Authentication. All sensitive data should be set via the JWT Authentication so a connecting client cannot tamper with it. The data has to be set as a JavaScript object.

Custom member data using JavaScript

Setting data

var drone = new ScaleDrone('channel_id', {
  data: {
    name: 'yellow_tree',
    color: '#fe21f1',
  }
});

Reading data

room.on('member_join', function(member) {
  console.log(member.clientData.name); //yellow_tree
  console.log(member.clientData.color); //#fe21f1
});

Custom member data using JWT Authentication

Setting data

Add a JSON data claim to the JWT.

{
  "client": "client_id_sent_from_javascript_client",
  "channel": "channel_id",
  "permissions": {
    ".*": {
      "publish": true,
      "subscribe": true
    }
  },
  "exp": 1408639878,
  "data": {
    "name": "green_sun",
    "color": "#00ff00"
  }
}

Reading data

room.on('member_join', function(member) {
  console.log(member.authData.name); //green_sun
  console.log(member.authData.color); //#00ff00
});