NAV
javascript

Introduction

Welcome to the SignalWire Chat documentation. This is an alpha release of our chat API.

Getting started

Creating a token

curl http://example.signalwire.com/api/chat/create_token \
  --request POST \
  --user "YourProjectID:YourAuthToken" \
  --header "Content-Type: application/json" \
  --data-raw '
    {
      "uuid": "luca-999",
      "info": {
        "full_name": "Luca Here"
      }
    }

The client instance

const _chat = new SignalWireChat({token: TOKEN, domain: DOMAIN});

_chat.subscribe('main', (response) => {
      _chat.getHistory('main', (response) => {
        console.log('history', response)
      });
    });

_chat.onMessage = function(msg) {
  setMessages(messages => [...messages, msg])
}

Messaging

Receive messages

Once you set up the handler, all messages will be received on this callback.

socket.onMessage = function(msg) {
  setMessages(messages => [...messages, msg])
}

The above callback receives a JS object structured like this:

{
  channel: "TestRoom", 
  sender: {
    uuid: "abc123",
    info: {
      full_name: "John Smith"
    }
  },
  payload: {
    text: "hello there",
    timestamp: "2021-04-28T12:24:13.443Z"
  }
}

Send a message

It is very easy to send a message to the room.

_chat.sendMessage('this is your message');

Loading history

The getHistory method will return the history for the specified channel.

_chat.getHistory('TestRoom', (response) => {
  console.log('history response', response)
});

The above callback receives a JS object structured like this:

[{
  channel: "TestRoom", 
  sender: {
    uuid: "abc123",
    info: {
      full_name: "John Smith"
    }
  },
  payload: {
    text: "hello there",
    timestamp: "2021-04-28T12:24:13.443Z"
  }
},{
  channel: "TestRoom", 
  sender: {
    uuid: "def456",
    info: {
      full_name: "Frank Green"
    }
  },
  payload: {
    text: "hello too",
    timestamp: "2021-04-29T12:24:13.443Z"
  }
}]

Sending private messages

To send a private message, use the sendPrivate method passing in the UUID for the destination user and the payload.

_chat.sendPrivate('def456', { text: 'hey there in private' });

Receiving private messages

Every client is automatically subscribed to a private channel for 1-1 messaging.

To trigger a callback when receiving a private message, use onPrivate:

_chat.onPrivate = function(msg){
  console.log(msg);
}

The onPrivate callback receives a JS object structured like this:

{
  sender: {
    uuid: "abc123",
    info: {
      full_name: "John Smith"
    }
  },
  payload: {
    text: "hello there",
    timestamp: "2021-04-28T12:24:13.443Z"
  }
}

Presence

Getting the current list

The SDK allows you to retrieve the list of people that are present in a channel. Presence is usually implemented as a special type of channel where every user subscribes.

For example, the following example uses getPresence to retrieve the users who are in the channel named presence (it could have any other name) as soon as the subscription succeeds.

_chat.subscribe('presence', (response) => {
  _chat.getPresence('presence', (response) => {
    console.log('presenre resp', response)
  });
});

the getPresence command returns the following JS object:

{
  channel: 'presence',
  presence: [{
    uuid: 'abc123',
    info: {
      full_name: "John Smith"
    }
  }]
}

Reacting to a presence event

If you set up an onPresence handler, you will get presence events from every channel you are subscribed to.

_chat.onPresence = function(msg){
  console.log(msg);
}

the onPresence callback returns the following JS object:

{
  channel: 'presence,
  uuid: 'abc123',
  info: {
    full_name: "John Smith"
  }
  event: 'joined'
}

Errors

Coming soon