Skip to content

Publish Subscribe on the Web side

Guillaume Gendre edited this page Jun 7, 2019 · 9 revisions

This page is part of the Cobalt Publish/Subscribe pages. Don't miss the others.

Using Cobalt Publish

Publish a JSON message to the specified channel.

cobalt.publish( channel, message )

  • channel : (string) The name of the channel in which you want to send your message.
  • message : (JSON object) The message that will be sent in the channel.

Example :

cobalt.publish("myChannel",{
   myValue : 42,
   anotherValue : "foo"
});

Using Cobalt Subscribe

Subscribe the current WebView to messages in the specified channel.

cobalt.subscribe( channel, callback)

  • channel : (string) The name of the channel you want to subscribe.
  • callback : (function) A javascript function to handle the messages when they'll come. This function receives one parameter : the message sent (JSON object).

Example :

cobalt.subscribe("myChannel",function(message){
   cobalt.log('received something on myChannel', message);
});

Note :

You can only set one listener for your webview. if you subscribe multiple time, only the latest listener will be called.

Using Cobalt Unsubscribe

Unsubscribe the current WebView to messages from the specified channel.

cobalt.unsubscribe( channel )

  • channel : (string) The name of the channel you want to unsubscribe.

Example :

cobalt.unsubscribe("myChannel");
Clone this wiki locally