Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 5122626

Browse files
committed
Update Javascript Documentation “websocket-connection/index”
1 parent b407759 commit 5122626

File tree

1 file changed

+56
-4
lines changed
  • docs/languages/javascript/websocket-connection

1 file changed

+56
-4
lines changed

docs/languages/javascript/websocket-connection/index.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
title: Websocket Connection
33
sidebar_label: Websocket connection
44
sidebar_position: 1
5+
tags:
6+
- javascript
7+
keywords:
8+
- js
9+
- websocket-connection
10+
description: how to setup websocket connection with deriv api?
511
---
612

713
:::caution
@@ -68,7 +74,7 @@ Now open the `index.html` file in our browser and checkout your Developer Consol
6874
Our websocket server provide `ping / pong` functionality, let's use it in our demo project to send and receive data. change the event listeners for `open` and `message` like so:
6975

7076
:::caution
71-
The `send` function on websocket connection only receives `string`, `ArrayBuffer`, `Blog` and `TypedArray`, `DataView`, you can read more abou them [on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send), which mean if we want to send an `object` we have stringify with `JSON.stringify` it first.
77+
The `send` function on websocket connection only receives `string`, `ArrayBuffer`, `Blob` and `TypedArray`, `DataView`, you can read more abou them [on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/send), which mean if we want to send an `object` we have stringify with `JSON.stringify` it first.
7278
:::
7379

7480
```js title="index.js"
@@ -113,9 +119,55 @@ By Defualt `Websocket connection` will be closed when no traffic is sent between
113119
Simple example setup would be like so:
114120

115121
```js title="index.js"
116-
const interval = 12000; // it's in milliseconds, which equals to 120 seconds
117-
setInterval(() => {
122+
const ping_interval = 12000; // it's in milliseconds, which equals to 120 seconds
123+
websocket.addEventListener('open', (event) => {
124+
console.log('websocket connection established: ', event);
125+
const sendMessage = JSON.stringify({ ping: 1 });
126+
websocket.send(sendMessage);
127+
128+
// to Keep the connection alive
129+
setInterval(() => {
130+
const sendMessage = JSON.stringify({ ping: 1 });
131+
websocket.send(sendMessage);
132+
}, ping_interval);
133+
});
134+
```
135+
136+
Now when the connection is `established` we start sending `ping` requests with `12000ms` intervals.
137+
138+
your final code should be:
139+
140+
```js title="index.js"
141+
const app_id = 1089; // Replace with your app_id or leave as 1089 for testing.
142+
const websocket = new WebSocket(`wss://ws.binaryws.com/websockets/v3?app_id=${app_id}`);
143+
const ping_interval = 12000; // it's in milliseconds, which equals to 120 seconds
144+
145+
// subscribe to `open` event
146+
websocket.addEventListener('open', (event) => {
147+
console.log('websocket connection established: ', event);
118148
const sendMessage = JSON.stringify({ ping: 1 });
119149
websocket.send(sendMessage);
120-
}, interval);
150+
151+
// to Keep the connection alive
152+
setInterval(() => {
153+
const sendMessage = JSON.stringify({ ping: 1 });
154+
websocket.send(sendMessage);
155+
}, ping_interval);
156+
});
157+
158+
// subscribe to `message` event
159+
websocket.addEventListener('message', (event) => {
160+
const receivedMessage = JSON.parse(event.data);
161+
console.log('new message received from server: ', receivedMessage);
162+
});
163+
164+
// subscribe to `close` event
165+
websocket.addEventListener('close', (event) => {
166+
console.log('websocket connectioned closed: ', event);
167+
});
168+
169+
// subscribe to `error` event
170+
websocket.addEventListener('error', (event) => {
171+
console.log('an error happend in our websocket connection', event);
172+
});
121173
```

0 commit comments

Comments
 (0)