Samsung Electronics logo

Managing Message Ports



Learning how to send messages to and receive responses from other webapis applications through message ports is a basic data communication skill:

  1. To retrieve the object instance of the RemoteMessagePort interface, use the requestRemoteMessagePort() method of the webapis.messageport object:

    var remotePort = webapis.messageport.requestRemoteMessagePort("r9vrpxzuyp.HybridServiceApp", 
                                                                "SAMPLE_PORT");
  2. Use the sendMessage() method of the RemoteMessagePort interface to send a message:

    remotePort.sendMessage(
    [
       {key:"command", value:"begin"},
       {key:"data", value:"dummy"}
    ]);

    If you expect a response message, pass the object instance of the LocalMessagePort interface as a second parameter of sendMessage() method:

    remotePort.sendMessage([<Message port data>], localPort);
  3. To receive responses, retrieve the object instance of the LocalMessagePort interface using the requestLocalMessagePort() method of the webapis.messageport object:

    var localPort = webapis.messageport.requestLocalMessagePort("SAMPLE_PORT_RESPONSE");
  4. Use the addMessagePortListener() method of the LocalMessagePort interface to add a callback method that is invoked when the response message arrives:

    var localPortWatchId = localPort.addMessagePortListener(function(data, remote) 
    {
       data.forEach(function(item) 
       {
          console.log('key:' + item.key + ' / value:' + item.value);
       });
    });