Skip to content

How to use DevicePubSub library

HunseopJeong edited this page Jul 13, 2021 · 4 revisions
  1. Sample App Configuration (Package ID is OZBS6gG8Jl in this example)

config.xml

<?xml version="1.0" encoding="UTF-8"?>
    <widget xmlns:tizen="http://tizen.org/ns/widgets" xmlns="http://www.w3.org/ns/widgets" id="http://yourdomain/Single" version="1.0.0" viewmodes="maximized">
    <tizen:application id="OZBS6gG8Jl.Sample" package="OZBS6gG8Jl" required_version="6.0"/>
    <content src="client.html"/>
    <feature name="http://tizen.org/feature/screen.size.all"/>
    <icon src="icon.png"/>
    <tizen:metadata key="d2dservice" value="enable"/>
    <name>Sample</name>
    <access origin="*" subdomains="true"></access>
    <tizen:privilege name="http://tizen.org/privilege/internet"/>
</widget>
  1. Local client code (Package ID is OZBS6gG8Jl in this example)

index.html

<head>
    <title>Local Device</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <script src="shared/res/client/js/socket.io.min.js"></script>
    <script src="shared/res/client/js/devicePubSub.js"></script>
    <script src="js/main.js"></script>
</head>

js/main.js

var devicePubSub= new DevicePubSub('OZBS6gG8Jl', onSubscribe);
function onSubscribe(msg) {
    if (msg.type == "remote publishing") {
        // TODO: Implement your code
        console.log(`data : ${msg.data}`);
    }
}
devicePubSub.publish('local publishing', {data : 'topic from local device'});
  1. Remote client code (Package ID is OZBS6gG8Jl in this example)

shared/res/client/client.html

<head>
    <title>Remote Device</title>
    <meta content="width=device-width, user-scalable=no" name="viewport" />
    <script src="js/socket.io.min.js"></script>
    <script src="js/jsencrypt.js"></script>
    <script src="js/devicePubSub.js"></script>
    <script src="js/client.js"></script>
</head>

shared/res/client/js/client.js

var devicePubSub = new DevicePubSub('OZBS6gG8Jl', onSubscribe);
function onSubscribe(msg) {
    if (msg.type == "local publishing") {
        // TODO: Implement your code
        console.log(`data : ${msg.data}`);
    }
}
devicePubSub.publish('remote publishing', {data : 'topic from remote device'});

Clone this wiki locally