diff --git a/Network/Application/Application.md b/Network/Application/Application.md index c55447e609..8ecb139a8a 100644 --- a/Network/Application/Application.md +++ b/Network/Application/Application.md @@ -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) diff --git a/Network/Application/web/web.md b/Network/Application/web/web.md new file mode 100644 index 0000000000..b30ac77cfe --- /dev/null +++ b/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); +}); +``` diff --git a/Network/Network.md b/Network/Network.md index 5fa1239bc2..4b03318112 100644 --- a/Network/Network.md +++ b/Network/Network.md @@ -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) diff --git a/Programming/JavaScript/JavaScript.md b/Programming/JavaScript/JavaScript.md index 1bf510e54f..4ce773d29e 100644 --- a/Programming/JavaScript/JavaScript.md +++ b/Programming/JavaScript/JavaScript.md @@ -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 diff --git a/SUMMARY.md b/SUMMARY.md index c62acfd59e..2a6cf143fa 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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