top

JavaScript Sender App

JavaScript API can be used for web sites running on mobile or desktop web browsers. It is compatible with any modern browsers that support web sockets.
JavaScript API supports ONLY Chrome , FireFox , Opera.

Prerequisites

Include the library in your SmartTV applications HTML page

<!DOCTYPE html>
<html>
<head lang="en">
    <!-- Add the script to your html (exposes a `msf` object to the window) -->
    <script src="scripts/vendor/msf-2.0.x.min.js"></script>
</head>
...
</html>

API Usage

Discovery

In order for a client to start or join a MultiScreen experience, the client application must be able to discover a compatible SmartTV service. The API manages searching for compatible services and only requires a few lines of code. Below is a chunk of code from the Hello World mobile web application

Important

JS Libary version 2.3.3 is changed search API. It works on only Chrome, Firefox, Opera except IE, Safari

Example :

var search = msf.search();

search.on('found', function(services){
    for(var i=0; i<services.length; i++){
        console.log('Found service '+services[i].name);
    }
});

search.start();

Connect

Once a compatible SmartTV is discovered, you can launch and connect to your SmartTV application from you mobile client. Application management is centralized to a single class (Application) that handles starting, stopping, installing, and connecting to your application. Here is a quick example.

Example :

// The first argument is the application id or hosted url
// The second is the channel (communication) id you will use for your application
var app = service.application('http://mydomain.com/myapp','com.mydomain.myapp');

// App connect supports callback and event listener coding styles
app.connect({name: 'John Doe'}, function (err) {
    if(err) return console.error(err);
    console.log('callback : you are connected');
});

Communicate

Once your client has started and established a connection with the SmartTV application, You can use the applications channel to send and receive messages between the devices. The Application class extends Channel, so in the example below we will continue with application instance we created earlier. Applications also provide notifications when someone connects, disconnects, and publishes a message.

Example:

app.on('clientConnect', function(client){
    // This will publish the say event to the client that connected 
    app.publish('say', 'Hello '+client.attributes.name, client.id);
});

// This event will fire when another client publishes the `say` event to a target including your client

app.on('say', function(msg, fromClient){
    console.log(fromClient.attributes.name + ' says, ' + msg);
});