Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between channels and keys

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?

like image 634
Brandon Wamboldt Avatar asked Mar 24 '23 03:03

Brandon Wamboldt


1 Answers

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

like image 136
Slukehart Avatar answered Apr 02 '23 06:04

Slukehart