You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 13, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/core-concepts/websocket/index.md
+20-19Lines changed: 20 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,13 @@ keywords:
14
14
description: What is a WebSocket?
15
15
---
16
16
17
-
## What is a WebSocket?
17
+
## What are WebSockets?
18
18
19
-
The `WebSocket` protocol, described in the specification [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455), provides a way to exchange data between browser and server via a persistent connection. The data can be passed in both directions as “packets”, without breaking the connection and the need of additional HTTP-requests.
19
+
The `WebSocket` protocol, described in the specification [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455), provides a way to exchange data between the browser and the server via a persistent connection. The data can be passed in both directions as “packets” without breaking the connection or needing additional HTTPrequests.
20
20
21
21
WebSocket is especially great for services that require continuous data exchange, e.g. real-time trading systems and so on.
22
22
23
-
## A Simple example
23
+
## A simple example
24
24
25
25
To open a WebSocket connection, we need to create `new WebSocket` using the special protocol `ws`or `wss` in the url. Here is how you can do that in `JavaScript`:
26
26
@@ -29,21 +29,21 @@ let socket = new WebSocket('wss://ws.binaryws.com/websockets/v3?app_id=1089');
29
29
```
30
30
31
31
:::caution
32
-
Always prefer `wss://`. The `wss://` protocol is not only encrypted, but also more reliable.
32
+
Using `wss://` is always the better choice. The `wss://` protocol is not only encrypted, but also more reliable.
33
33
34
-
That’s because ws:// data is not encrypted, visible for any intermediary. Old proxy servers do not know about WebSocket, they may see “strange” headers and abort the connection.
34
+
On the other hand, the `ws://` data is not encrypted and can be visible to intermediaries. Old proxy servers may encounter "strange" headers and terminate the connection.
35
35
36
-
On the other hand, `wss://`is WebSocket over TLS, (same as HTTPS is HTTP over TLS), the transport security layer encrypts the data at the sender and decrypts it at the receiver. So data packets are passed encrypted through proxies. They can’t see what’s inside and let them through.
36
+
`wss://`stands for WebSocket over TLS, similar to how HTTPS is HTTP over TLS. With the transport security layer, data is encrypted by the sender and decrypted by the receiver. This means that encrypted data packets can successfully pass through proxies without being inspected.
37
37
:::
38
38
39
-
Once the socket is created, we should listen to events on it. There are totally 4 events:
39
+
Once the socket is created, we should listen to events on it. There are 4 events altogether:
40
40
41
-
-open – connection established,
42
-
-message – data received,
43
-
-error – WebSocket error,
44
-
-close – connection closed.
41
+
-Open – Connection established
42
+
-Message – Data received
43
+
-Error – WebSocket error
44
+
-Close – Connection closed
45
45
46
-
And if we’d like to send a message then socket.send(data) will do that.
46
+
Sending a message can be done via socket.send(data).
47
47
48
48
Here’s an example in `JavaScript`:
49
49
@@ -79,17 +79,18 @@ socket.onerror = function (error) {
79
79
80
80
## Why do we need WebSockets and when should we avoid them?
81
81
82
-
WebSocket are an essential client-server communication tool and one needs to be fully aware of its utility and avoid scenarios to benefit from its utmost potential. It’s explained extensively in the next section.
82
+
WebSocket are an essential client-server communication tool. To benefit the most from their potential, it's important to understand how they can be helpful and when it's best to avoid using them. It’s explained extensively in the next section.
83
83
84
84
Use WebSocket When You Are:
85
85
86
-
1. Developing real-time web application
87
-
The most customary use of WebSocket is in real-time application development wherein it assists in a continual display of data at the client end. As the backend server sends back this data continuously, WebSocket allows uninterrupted pushing or transmitting this data in the already open connection. The use of WebSockets makes such data transmission quick and leverages the application's performance.
88
-
2. A real-life example of such WebSocket utility is in the trading websites such as deriv. Here, WebSocket assist in data handling that is impelled by the deployed backend server to the client.
89
-
3. Creating a chat application
90
-
Chat application developers call out WebSocket for help in operations like a one-time exchange and publishing/broadcasting the messages. As the same WebSocket connection is used for sending/receiving messages, communication becomes easy and quick.
86
+
1. When you're developing a real-time web application.
87
+
The most customary use of WebSocket is in real-time application development wherein it assists in a continual display of data at the client end. As the back-end server sends back this data continuously, a WebSocket allows uninterrupted pushing or transmitting of this data in the already open connection. The use of WebSockets makes such data transmission quick and leverages the application's performance.
88
+
2. For trading websites, such as Deriv.
89
+
Here, WebSocket assist in data handling that is impelled by the deployed back-end server to the client.
90
+
3. When creating a chat application.
91
+
Chat application developers call out WebSockets for help in operations like a one-time exchange and publishing/broadcasting messages. As the same WebSocket connection is used for sending/receiving messages, communication becomes easy and quick.
91
92
92
-
Now that it’s clear where WebSocket should be used, don’t forget to know the cases where it should be avoided and keep yourself away from tons of operational hassles.
93
+
Now that we've established where WebSockets should be used, let's see where it is best to avoid them. This will help you steer clear of unnecessary operational hassles.
93
94
94
95
WebSocket shouldn’t be taken onboard when old data fetching is the need of the hour or need data only for one-time processing. In these cases, using HTTP protocols is a wise choice.
0 commit comments