Mobile Web Wearable Web

Message Port: Communicating with Other Applications

This tutorial demonstrates how you can send and receive messages through message ports.

The Message Port API is mandatory for both Tizen mobile and wearable profiles, which means that it is supported in all mobile and wearable devices. All mandatory APIs are supported on the Tizen Emulators.

Warm-up

Become familiar with the Message Port API basics by learning about:

Managing Message Ports

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

  1. To create a local port which receives messages, use the requestLocalMessagePort() method of the LocalMessagePort interface (in mobile and wearable applications):

    var localPort = tizen.messageport.requestLocalMessagePort("SAMPLE_PORT");
    
  2. To retrieve an instance of the RemoteMessagePort interface (in mobile and wearable applications), use the requestRemoteMessagePort() method of the tizen.messageport object. The RemoteMessagePort interface sends messages to the port identified by an ApplicationId and a port name.

    var targetApplicationId = tizen.application.getCurrentApplication().appInfo.id;
    var remotePort = tizen.messageport.requestRemoteMessagePort(targetApplicationId, "SAMPLE_PORT");
    
  3. Use the addMessagePortListener() method of the LocalMessagePort interface to add a callback method that is invoked when the message arrives:

    var localPortWatchId = localPort.addMessagePortListener(function(data, replyPort) 
    {
       for (var i = 0; i < data.length; i++)
       {
          console.log("key:" + data[i].key + " / value:" + data[i].value);
       }
       if (replyPort) 
       {
          console.log("replyPort given: " + replyPort.messagePortName);
       }
    });
    
  4. Use the sendMessage() method of the RemoteMessagePort interface to send a message:

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

    If you expect a response message, pass the instance of the LocalMessagePort interface as a second parameter of sendMessage() method to specify the message port where the response is sent:

    remotePort.sendMessage(messageData, localPort);
    

To implement the application receiving the message as a native application, see the mobile native Message Port Tutorial.

Go to top