What is the difference between channels and keys

269 views Asked by At

I'm developing an app with GoInstant, but the difference between keys and channels isn't very clear. When should I use keys vs channels?

1

There are 1 answers

0
Slukehart On BEST ANSWER

Keys: As in key-value store, the Key object is the interface by which you manage and monitor a value in GoInstant. You should use them for CRUD (Create, Read, Update Delete).

key example:

// We create a new key using our room object
var movieName = yourRoom.key(‘movieName’);

// Prepare a handler for our `on` set event
function setHandler(value) {
    console.log(‘Movie has a new value’, value);
}

// Now when the value of our key is set, our handler will fire
movieName.on(‘set’, setHandler);

// Ready, `set`, GoInstant :)
movieName.set('World War Z', function(err) {
    if (!err) alert('Movie set successfully!')
}

Channels: Represent a full-duplex messaging interface. Imagine a multi-client pub/sub system. Channels do not store data, you can’t retrieve a message from a channel, you can only receive it. You should use it to propagate events between clients sharing a session.

channel example:

var mousePosChannel = yourRoom.channel('mousePosChannel');

// When the mouse moves, broadcast the mouse co-ordinates in our channel
$(window).on('mousemove', function(event) {
  mousePosChannel.message({
    posX: event.pageX,
    posY: event.pageY
  });
});

// Every client in this session can listen for changes to
// any users mouse location
mousePosChannel.on('message', function(msg) {
  console.log('A user in this room has moved there mouse too', msg.posX, msg.posY);
})

You can find the official docs here:

Key: https://developers.goinstant.net/v1/key/index.html

Channel: https://developers.goinstant.net/v1/channel/index.html