Skip to content

Message Specification

cachafla edited this page Mar 11, 2011 · 4 revisions

This is a work in progress...

Connect

This is the initial message to be sent to a server. It is because when the client first connects to the server, it should try to recover an active session with the server so that it doesn't lose the active XMPP connection that was previously established. Since the session is initialized by express on page load, there should be a readable cookie on application start. This cookie needs to be sent to the websocket server so the client can identify itself. The websocket server will then use 2 methods to identify a client: cookies or sessionId. See the following example:

// The client tries to identify itself providing the express session cookie
{
  type : 'connect',
  sid  :  'value-of-cookie'
}

When the websocket server receives this message, it will try to find an existing XMPP connection for this session or the corresponding sessionId (websocket session). If there is an existing session for the client, the server will respond with the connect-ok message. If it doesn't, it will respond with connect-not-ok:

// Successful reconnection
{
  type : 'connect-ok'
}

// Failed reconnection
{
  type : 'connect-not-ok'
}

In the event that the reconnection wasn't successful, the client should authenticate against the server.

Authenticate

When a client receives a connect-not-ok response from the server, it should issue an authentication message to authenticate the connection against a jabber server:

{
  type     : 'auth',
  jid      : '[email protected]',
  password : '123456',
  sid      :  'value-of-cookie'
}

There is a reason for specifying the session identifier again. With this value, then server can then store the XMPP session object so it can be reused within the expiration window of the cookie, provided the user hasn't manually logged out from the session. The response for this message includes a auth-ok type, and it indicates the client that the XMPP session has been established. If there was an authentication failure, the server will respond with a auth-not-ok message type.

Join Room

Once the client receives a connect-ok acknowledgement from the server it may join a chatroom. The following is a sample request to join a room:

{
  type : 'join-room',
  room : '[email protected]'
}

The server then responds with a join-room-ok message:

{
  type : 'join-room-ok',
  room : '[email protected]'
}

Message

This is the interface for sending and receiving messages in chatrooms. The interface for this is self explanatory, but one should note that the from and to fields can refer to chatrooms or jids (Jabber Ids/Users) depending on wether the messages comes from the client or goes to the jabber server.

{
  type : 'message',
  from : '[email protected]',
  to   : '[email protected]',
  body : 'hello!',
}

The server provides and acknowledgment message in case the client waits for confirmation:

{
  type : 'message-ok'
}

Presence

TODO Cache members per room in the server so they can be sent to the client on reconnection

Whenever a user joins to a chatroom, a presence event is triggered. Right now this is the only event that is generated from the websocket server. The client can then maintain a list of active users in the room and notify about people logging in / out of it.

// At this moment status can only be online or offline. This attribute will represent more 
// accurate status descriptions for the joining user such as idle, away, busy, etc.
{
  type   : 'presence',
  to     : '[email protected]',
  from   : '[email protected]',
  status : 'online'
}
Clone this wiki locally