# Chat Service

The Chat Service provides the ability to:

- Send messages to a room
- Receive messages from a room
- Get a room's chat message history

All room members that are subscribed to the chat will receive the text messages. You must first enter the room before you may send and receive messages.

## Initializing

First, instantiate the chat service. The [Room Service](/content/docs/sdk_ref/web/room/#object-roomservice/index.html) exposes a method to get the chat associated with the active room.

```javascript
var chatService = roomService.getChatService();
```

### [object ChatService]

| Name | Signature | Returns | Description |
| --- | --- | --- | --- |
| start | (batchSize) | void | Start receiving messages and store the most recent of size batchSize |
| stop | () | void | Stop receiving messages |
| getObservableChatMessages | () | Observable(of Array) | Get the observable of the most recent chat messages. This is a buffer of the most recent N messages up to a max of 100. See [Listen for Chat Messages](/content/docs/sdk_ref/web/chat/#listen-for-chat-messages-in-the-active-room/index.html) |
| getObservableChatEnabled | () | Observable(of Boolean) | Get the observable of the enabled status of the chat service. Use this to disable or enable the chat. |
| sendMessageToRoom | (message, callback) | void | [Send a message to room](/content/docs/sdk_ref/web/chat/#send-a-message-to-the-active-room/index.html). |
| getMessages | (batchSize, afterMessageId, beforeMessageId, callback) | Array(of [object ChatMessage]) | [Get chat message history](/content/docs/sdk_ref/web/chat/#chat-message-history/index.html) |

### [object ChatMessage]

| Name | Type | Description |
| --- | --- | --- |
| messageId | String | Unique ID of message |
| timestamp | Number | Server UTC timestamp of message |
| from | [object ChatMessageMember] | Information on the member that sent the message |
| message | String | Chat message |

### [object ChatMessageMember]

| Name | Type | Description |
| --- | --- | --- |
| sessionId | String | Member's session ID |
| screenName | Number | Screen name of the member |
| role | String | Member's role. See [Member Roles](/content/docs/sdk_ref/web/room/#member-roles/index.html) |
| lastUpdate | Number | Last time member was updated as UTC timestamp |

## Listen for Chat Messages in the Active Room

The chat service exposes an observable of the most recent N (batchSize) messages in the active room up to a max of 100 messages. To view older messages you will need to use the [getMessages](/content/docs/sdk_ref/web/chat/#chat-message-history/index.html) method.

```javascript
var chatService = new roomService.getChatService();
var chatMessageObservable = chatService.getObservableChatMessages();
var batchSize = 10;

chatMessageObservable.subscribe(function (mostRecentChatMessages) {
  // Do something with chat messages
  // mostRecentChatMessages is an array of messages
  // The messages in the array are not guaranteed to be in order, and may contain duplicates
});

chatService.start(batchSize);
```

## Send a Message to the Active Room

Send a single message to the active room. If the message is successfully sent the chatMessageObservable will trigger notifications on all subscribers with the new chat message.

```javascript
var chatService = new roomService.getChatService();

chatService.start(batchSize);

chatService.sendMessageToRoom(
  'My First Message',
  function sendMessageCallback(error, response) {
    if (error) {
      // handle error - send message again
    }

if (response.status !== 'ok') {
      // handle error - send message again
    }

if (response.status === 'ok') {
      // Successfully sent message
    }
  }
);
```

### Send Message Parameters

| Name | Type | Description |
| --- | --- | --- |
| message _(required)_ | String | Message to send to the room |
| sendMessageCallback _(required)_ | Function | Callback with the status of the request |

### Send Message Callback Arguments

| Name | Type | Description |
| --- | --- | --- |
| status | String | The status of the operation |

### Send Message Callback Status Codes

| Status | Description |
| --- | --- |
| ok | Send message succeeded |
| _varies_ | Send message failed for other reasons |

## Chat Message History

Get messages older than the most recent N buffered in the chatMessageObservable.

> Get the 10 most recent messages after a message:

```javascript
var batchSize = 10;
var afterMessageId = 'MessageId1';
var beforeMessageId = null;

chatService.getMessages(
  batchSize,
  afterMessageId,
  beforeMessageId,
  function getMessagesCallback(error, response) {
    if (error) {
      // handle error - request messages again
    }

if (response.status !== 'ok') {
      // handle error - request messages again
    }

if (response.status === 'ok' && response.chatMessages) {
      // Successfully got chat messages
    }
  }
);
```

> Get all messages between two messages:

```javascript
var batchSize = null;
var afterMessageId = 'MessageId1';
var beforeMessageId = 'MessageId2';

if (response.status !== 'ok') {
      // handle error - request messages again
    }

if (response.status === 'ok' && response.chatMessages) {
      // Successfully got chat messages
    }
  }
);
```

### Get Messages Parameters

| Name | Type | Description |
| --- | --- | --- |
| batchSize _(optional)_ | Number | Limit of messages to receive. Used if either afterMessageId or beforeMessageId are provided |
| afterMessageId _(optional)_ | String | The messageId to return after. The N messages after message with ID of afterMessageId but before beforeMessageId |
| beforeMessageId _(optional)_ | String | The messageId to return before. The N messages before message with ID of beforeMessageId |
| getMessagesCallback _(required)_ | Function | Callback with the status of the request |

### Get Messages Callback Arguments

| Name | Type | Description |
| --- | --- | --- |
| status | String | The status of the operation |
| chatMessages | [[array ChatMessage]](/content/docs/sdk_ref/web/chat/#object-chatmessage/index.html) | Array of chat messages matching search criteria |

### Get Messages Callback Status Codes

| Status | Description |
| --- | --- |
| ok | Get messages succeeded |
| _varies_ | Get messages failed for other reasons |
