MAF.Room

When you need to communicatie between different users of your app or between your app and a phone. The communication will take place through sockets.

new MAF.Room(id)

Parameters:

NameTypeDescription
idString

Identifier to be used to construct the unique room hash.

Members

connected :Boolean static

When your connected to the server it returns true

hash :String static

Unique hash that defines the room id.

joined :Boolean static

When your connected to the room it returns true

Methods

destroy(data)

Destroy the room. If your still connected it will disconnect.

Parameters:

NameTypeArgumentDescription
dataObjectoptional 

Possibility to send some data to the room before disconnecting.

join(data)

Join the room

Parameters:

NameTypeArgumentDescription
dataObjectoptional 

Data to be broadcasted to every connection to the room.

leave(data)

Leave the room.

Parameters:

NameTypeArgumentDescription
dataObjectoptional 

Data to be broadcasted to every connection to the room.

send(data)

Send data to the room.

Parameters:

NameTypeDescription
dataObject

Data to be broadcasted to every connection to the room.

Events

The events on this Instance are subcribed through the function.prototype.subscribeTo method.

var room = new MAF.Room('MyUniqueIdentifier');
(function (event) {
   console.log(event.type, event.payload);
}).subscribeTo(room, ['onConnected', 'onDisconnected', 'onCreated', 'onDestroyed', 'onJoined', 'onHasLeft', 'onData', 'onError']);
room.join();

onConnected

Fired when you connect to the room. The creator of the room wil not recieve this event.

onCreated

Fired only when the room is created.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash.

userString

The user id that joined the room.

dataMixed

Data send along when joining the room.

var room = new MAF.Room('MyUniqueIdentifier');
if (room.users.length > 0)
   room.join({msg: 'Client joined the room.'});
else
   room.join({msg: 'Room is created.'});

onData

Fired when someone sends some data to the room.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash.

userString

The user id that joined the room.

dataMixed

Data send by a user in the room.

onDestroyed

Fired when the room is destroyed.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash.

userString

The user id that joined the room.

dataMixed

Data that was send to the room when leaving the room.

onDisconnected

Fired when you disconnect from the room.

onError

Fired when there is a error.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash. Depending on the error this can be empty.

userString

The user id that triggered this error, this can be empty depending on the error.

codeMixed

The error code. Possible errors:

  • 404 - Room doesn't exists.
  • 405 - Not allowed to create Room.
  • 429 - Too many connections.

onHasLeft

Fired when someone left the room.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash.

userString

The user id that joined the room.

dataMixed

Data that was send to the room when leaving the room.

Example

//Handling things when users leave.
var room = new MAF.Room('MyUniqueIdentifier');
(function (event) {
   if (event.payload.user !== room.user) {
      console.log('A user has left the room:', event.payload.user, event.payload.data);
   } else {
      console.log('You have left the room.', event.payload.user, event.payload.data);
   }
}).subscribeTo(room, ['onHasLeft']);

//After being connected.
room.leave('bye bye');

onJoined

Fired when someone has joined the room. This also includes the user that creates the room.

Parameters:

NameTypeDescription
eventObject

The event object.

Properties
NameTypeDescription
typeString

Event type.

payloadObject

Payload object containing event data.

Properties
NameTypeDescription
hashString

The room hash.

userString

The user id that joined the room.

dataMixed

Data send along when joining the room.

Example

//Handling things when users join.
var room = new MAF.Room('MyUniqueIdentifier');
(function (event) {
   if (event.payload.user !== room.user) {
   	console.log('A user has joined the room:', event.payload.user, event.payload.data);
   } else {
      console.log('You have joined the room.', event.payload.user, event.payload.data);
   }
}).subscribeTo(room, ['onJoined']);
room.join('Hello');