Skip to content

Commit

Permalink
doc: web communication
Browse files Browse the repository at this point in the history
  • Loading branch information
aleen42 committed Nov 24, 2023
1 parent 59c0f99 commit 78f99ad
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Network/Application/Application.md
Expand Up @@ -3,3 +3,7 @@
- [HTTP](./http/http.md) (Hypertext Transfer Protocol)
- [DNS](./dns/dns.md) (Domain Name System)
- [RPC](./rpc/rpc.md) (Remote Procedure Call)

### Practices

- [Web Communication](./web/web.md)
31 changes: 31 additions & 0 deletions Network/Application/web/web.md
@@ -0,0 +1,31 @@
## Web Communication [Back](../Application.md)

In practices of web programming, we often need to communicate between the client and the server and provide immediate notifications from servers' push. There are three main ways to achieve it on the web: HTTP polling, Server-Sent Events (SSE), and web sockets.

HTTP polling means creating a fixed-time running task to poll messages from the server.

```js
setInterval(() => fetch(url), 100); // polling things each 100 ms
```

The Server-Sent Events means creating a single-simplex channel to grasp notifications from servers:

```js
(new EventSource(url)).onmessage = event => {
// handle the notication
console.log('Message from server', event.data);
};
```

Web sockets means creating a duplex channel to communicate with servers:

```js
const socket = new WebSocket(url);
socket.addEventListener('open', event => {
socket.send('hello');
});

socket.addEventListener('message', event => {
console.log('Message from server', event.data);
});
```
2 changes: 1 addition & 1 deletion Network/Network.md
Expand Up @@ -3,7 +3,7 @@
* [Overview](./Overview/Overview.md)
* [Coding](./Coding/Coding.md)

### 5 Layers - the structure of Network
### 5 Layers - the structure of Network

* [Application Layer](./Application/Application.md)
* [Transport Layer](./Transport/Transport.md)
Expand Down
1 change: 1 addition & 0 deletions Programming/JavaScript/JavaScript.md
Expand Up @@ -37,6 +37,7 @@ Or if you are advanced in this, you may be interested at [Ecma International, Te
* [**Generator**](./generator/generator.md)
* [**Reflect**](./reflect/reflect.md)
* [**Atomics**](./atomics/atomics.md)
* [**Web Communication**](../../Network/Application/web/web.md)

#### Advanced

Expand Down
2 changes: 2 additions & 0 deletions SUMMARY.md
Expand Up @@ -462,6 +462,8 @@
* [Fetch metadata request headers](Network/Application/http/fetch_metadata_headers/fetch_metadata_headers.md)
* [DNS](Network/Application/dns/dns.md)
* [RPC](Network/Application/rpc/rpc.md)
* Practices
* [Web Communication](Network/Application/web/web.md)
* [Transport Layer](Network/Transport/Transport.md)
* [Out of band(OOB, 帶外數據)](Network/Transport/OOB/OOB.md)
* Network Layer
Expand Down

0 comments on commit 78f99ad

Please sign in to comment.