|
2 | 2 | title: Websocket Connection
|
3 | 3 | sidebar_label: Websocket connection
|
4 | 4 | sidebar_position: 1
|
| 5 | +tags: |
| 6 | + - javascript |
| 7 | +keywords: |
| 8 | + - js |
| 9 | + - websocket-connection |
| 10 | +description: how to setup websocket connection with deriv api? |
5 | 11 | ---
|
6 | 12 |
|
7 | 13 | :::caution
|
@@ -68,7 +74,7 @@ Now open the `index.html` file in our browser and checkout your Developer Consol
|
68 | 74 | 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:
|
69 | 75 |
|
70 | 76 | :::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. |
72 | 78 | :::
|
73 | 79 |
|
74 | 80 | ```js title="index.js"
|
@@ -113,9 +119,55 @@ By Defualt `Websocket connection` will be closed when no traffic is sent between
|
113 | 119 | Simple example setup would be like so:
|
114 | 120 |
|
115 | 121 | ```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); |
118 | 148 | const sendMessage = JSON.stringify({ ping: 1 });
|
119 | 149 | 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 | +}); |
121 | 173 | ```
|
0 commit comments