Sending Messages

Getting a list of channels

In order to send a message to a channel, you first need a channel id to send a message to. To get a list of channels that the user belongs to you can use currentUser.getChannels. For this example, the first channel that is returned is stored as currentChannel.

var currentChannel;
voicelayer.currentUser.getChannels(function(err, data) {
  if (err) {
    alert("something went wrong " + data.error);
    return;
  }

  if (data.length === 0) {
    alert("No channels");
    return;
  }

  currentChannel = data[0];
});

In the above example you can see that the error is handled. This pattern can be used for most function calls. The err variable will be displayed in the rest of the examples, however the if (err) { ... } may be omitted to focus on the functionality specific to the example.

Sending a text message

To send a text message to the channel you can use channels.createMessage. This message will be broadcast to all users in the channel. The currentChannel variable from the previous example will be used.

var message = {
  text: "Hello World"
}

voicelayer.channels.createMessage(currentChannel, message, function(err, data) {
  var messageId = data.id;
  // do something with the message
});

Sending an audio message

Requesting mic access

The first step in sending an audio message is getting access to the users microphone. You can use recorder.isMicEnabled and recorder.requestMicAccess to do this.

if (!voicelayer.recorder.isMicEnabled()) {
  voicelayer.recorder.requestMicAccess(function(err) {
    if (err) {
      alert("Mic not enabled");
      return;
    }
    // Now ready for recording
  });
}

Recording a message

The currentChannel variable will be used again. The channels.recordMessage function can be used to start recording a message to the channel.

var messageId = voicelayer.channels.recordMessage(currentChannel);

Connected clients will receive a notification of this prompting them to play the message. This is discussed in Playing Messages.

In order to stop recording recorder.stop must to be called.

voicelayer.recorder.stop();

The duration of the last recorded message can be retrieved with recorder.getDuration.

var duration = voicelayer.recorder.getDuration();

Sending an attachment

An attachment can be sent to the channel with channels.createAttachment. It expects a file. One way to get a file is from a file uploader.

<input type="file" id="fileInput" />
var file = document.getElementById("fileInput").files[0];
voicelayer.channels.createAttachment(currentChannel, file, function(err, data) {
  //do something with the message
}

Updating a message

Messages can be updated with messages.update. In the audio message above, it is a good practice to set the duration after recording.

var message = { duration: duration };
voicelayer.messages.update(messageId, message, function(err, data) {
  //do something with updated message
})

Deleting a message

Messages can be updated with messages.delete.

voicelayer.messages.delete(messageId, function(err) {
})