Skip to content

Commit 7d3f71c

Browse files
authored
Merge pull request thingsboard#76 from dmytro-landiak/master
WebSocket API Example
2 parents 11c69a3 + 13e9cf6 commit 7d3f71c

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

docs/iot-gateway/mqtt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: MQTT protocol support for ThingsBoard IoT Gateway
1010
* TOC
1111
{:toc}
1212

13-
This guide will help you to get familiar with OPC-UA extension configuration for ThingsBoard IoT Gateway.
13+
This guide will help you to get familiar with MQTT extension configuration for ThingsBoard IoT Gateway.
1414
Use [general configuration](/docs/iot-gateway/configuration/) to enable this extension.
1515
We will describe extension configuration file below.
1616

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
5+
<script type="text/javascript">
6+
function WebSocketAPIExample() {
7+
var token = "YOUR_JWT_TOKEN";
8+
var entityId = "YOUR_DEVICE_ID";
9+
var webSocket = new WebSocket("ws(s)://host:port/api/ws/plugins/telemetry?token=" + token);
10+
11+
if (entityId === "YOUR_DEVICE_ID") {
12+
alert("Invalid device id!");
13+
webSocket.close();
14+
}
15+
16+
if (token === "YOUR_JWT_TOKEN") {
17+
alert("Invalid JWT token!");
18+
webSocket.close();
19+
}
20+
21+
webSocket.onopen = function () {
22+
var object = {
23+
tsSubCmds: [
24+
{
25+
entityType: "DEVICE",
26+
entityId: entityId,
27+
scope: "LATEST_TELEMETRY",
28+
cmdId: 10
29+
}
30+
],
31+
historyCmds: [],
32+
attrSubCmds: []
33+
};
34+
var data = JSON.stringify(object);
35+
webSocket.send(data);
36+
alert("Message is sent: " + data);
37+
};
38+
39+
webSocket.onmessage = function (event) {
40+
var received_msg = event.data;
41+
alert("Message is received: " + received_msg);
42+
};
43+
44+
webSocket.onclose = function (event) {
45+
alert("Connection is closed!");
46+
};
47+
}
48+
</script>
49+
50+
</head>
51+
<body>
52+
53+
<div id="sse">
54+
<a href="javascript:WebSocketAPIExample()">Run WebSocket</a>
55+
</div>
56+
57+
</body>
58+
</html>

docs/user-guide/telemetry.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,25 @@ where
141141
- **startTs** - start time of fetch interval for historical data query, in milliseconds.
142142
- **endTs** - end time of fetch interval for historical data query, in milliseconds.
143143

144-
A complete example is coming soon!
144+
##### Example
145+
146+
Change values of the following variables :
147+
148+
- **token** - to the JWT token which you can get using the [following link](https://thingsboard.io/docs/reference/rest-api/#rest-api-auth).
149+
150+
- **entityId** - to your device id.
151+
152+
In case of live-demo server :
153+
154+
- replace **host:port** with **demo-thingsboard.io** and choose secure connection - **wss://**
155+
156+
In case of local installation :
157+
158+
- replace **host:port** with **127.0.0.1:8080** and choose **ws://**
159+
160+
{% capture tabspec %}web-socket
161+
A,web-socket.html,html,resources/web-socket.html,/docs/user-guide/resources/web-socket.html{% endcapture %}
162+
{% include tabs.html %}
145163

146164
## Data visualization
147165

0 commit comments

Comments
 (0)