Skip to content

Instantly share code, notes, and snippets.

@kwhinnery
Last active Mar 2, 2016
Embed
What would you like to do?
var client = new Twilio.IPMessaging.Client('token');
// Creates a reference to a channel that may or may not exist
var channel = client.channel('general');
// If there's such a thing as an online/offline channel, this serves as the way the channel
// gets updated when it's reconnected from an offline state. It could also serve as a way
// to get updated when the channel information is initially fetched from the server
//
// Fired when:
// - channel data is initially fetched and updated
// - an event happens elsewhere - the server adds a member, another client leaves, etc
channel.on('update', function() {
console.log(channel.friendlyName);
console.log(channel.members);
});
// Here's a thing that absolutely must be async - your attempt to send a message can succeed or fail.
// developer needs to handle this for sure, and deal with the result
channel.sendMessage('hey there').then(function() {
console.log('sent message');
}).catch(function(err) {
console.log(err);
});
// here's a thing that may or may not happen in the future - we can listen for this kind of event,
// doesn't mean we'll ever get one!
channel.on('messageAdded', function(message) {
console.log(message.body);
});
// An error event might happen in cases where a channel exists, but you don't have access to it.
// Or you get kicked from the channel by the server, or whatever.
channel.on('error', function(err) {
console.log(err.code);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment