Skip to content

Commit 5e3331b

Browse files
committed
WIP: SDC Decomposition Sprint 3
1 parent 2d8690b commit 5e3331b

File tree

3 files changed

+186
-0
lines changed
  • common-content/en/module/decomposition
  • org-cyf-sdc/content/decomposition/sprints/3/prep

3 files changed

+186
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
+++
2+
title = "Adding like/dislike"
3+
headless = true
4+
time = 120
5+
facilitation = false
6+
emoji= "👍👎"
7+
objectives = [
8+
"Identify what data needs to be stored and exchanged between a client and server.",
9+
"Devise a scheme for differentiating messages with different meanings (e.g. a new message vs a new like).",
10+
"Contrast giving updated values as absolute values or relative changes.",
11+
"Implement an end-to-end feature involving data updates and reconciliation across a client and server.",
12+
]
13+
+++
14+
15+
The last requirement we have for our chat application is the ability to like/dislike a message (and see what messages have been liked/disliked).
16+
17+
{{<note type="Exercise">}}
18+
Think about what information a client would need to provide to a server in order to like/dislike a message.
19+
20+
Think about what information a server would need to provide to a client in order to display how many likes/dislikes a message has.
21+
22+
Think about what information a server would need to provide to a client in order to _update_ how many likes/dislikes a message has.
23+
24+
Write these things down.
25+
{{</note>}}
26+
27+
### Identifiers
28+
29+
One of the key new requirements to add liking/disliking a message is knowing _which_ message is being liked/disliked.
30+
31+
When a client wants to like a message, it needs some way of saying _this_ is the message I want to like.
32+
33+
This suggests we need a unique identifier for each message:
34+
* When the server tells a client about a message, it needs to tell it what the identifier is for that message.
35+
* When a client tells the server it wants to like a message, it needs to tell it the identifier for the message it wants to like.
36+
* When the server tells a client a message has been liked, it needs to tell the client which message was liked, and the client needs to know enough about that message to be able to update the UI.
37+
38+
### Message formats
39+
40+
Now that your server will be sending multiple kinds of updates ("Here's a new message", or "Here's an update to the number of likes of an existing message"), you'll need to make sure the client knows the difference between these messages. The client will need to know how to act when it receives each kind of message.
41+
42+
### Changes or absolutes?
43+
44+
When new likes happen, a choice we need to make is whether the server should tell a client "this message was liked again" or should tell the client "this message now has 10 likes". Both of these can work.
45+
46+
{{<note type="Exercise">}}
47+
Write down some advantages and disadvantages of a server -> client update being "+1 compared to before" or "now =10".
48+
49+
Choose which approach you want to take.
50+
{{</note>}}
51+
52+
{{<note type="Exercise">}}
53+
Implement liking and disliking messages.
54+
55+
If a message has a non-zero number of likes or dislikes, the frontend needs to show this.
56+
57+
The frontend needs to expose some way for a user to like or dislike any message.
58+
59+
When a user likes or dislikes a message, the frontend needs to tell the backend about this, and the backend needs to notify all clients of this.
60+
61+
When a frontend is notified by a backend about a new like or dislike, it needs to update the UI to show this.
62+
63+
You may do this in your polling implementation, WebSockets implementation, or both.
64+
{{</note>}}
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
+++
2+
title = "WebSockets"
3+
headless = true
4+
time = 120
5+
facilitation = false
6+
emoji= "🔌"
7+
objectives = [
8+
"Stream live updates from a server using WebSockets.",
9+
"Discuss the trade-offs of using WebSockets or polling.",
10+
"Describe properties of message formats (e.g. including a command name).",
11+
]
12+
+++
13+
14+
WebSockets are an API and protocol which allow creating a bi-directional communication channel between two programs.
15+
16+
They are commonly used in websites to establish a channel so that a backend can send updates to a frontend.
17+
18+
You can read [an introduction to WebSockets](https://docs.developer.tech.gov.sg/docs/data-engineering-initiative-playbook/Chapter5/Introduction_to_WebSockets), as well as roughly [what a client looks like](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications), and [what a server does](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers).
19+
20+
On the server side, we will be using the [websocket npm package](https://www.npmjs.com/package/websocket) which lists a server example in its README.
21+
22+
> [!TIP]
23+
>
24+
> This sprint, you will need to submit _both_ a copy of your code which supports polling, _and_ a copy which supports WebSockets.
25+
>
26+
> You probably want to make a copy of your polling code, and have two separate (similar) pages in your repo.
27+
28+
On the backend, you can create a WebSocket server by adding this code:
29+
30+
```js
31+
import { server as WebSocketServer } from "websocket";
32+
const server = http.createServer(app);
33+
const webSocketServer = new WebSocketServer({ httpServer: server });
34+
```
35+
36+
You will then need to follow the example in the `websocket` npm package's documentation to have your server handle requests.
37+
38+
On the client-side, you will need to make a new `WebSocket` connection to the server.
39+
40+
Some things to think about when implementing WebSockets updates:
41+
42+
### Learn new APIs in isolation
43+
44+
It will be easier for you to learn a new API (like WebSockets) with a simple example.
45+
46+
At the end, you will want your WebSocket to stream new messages from the server to the client, but maybe to explore WebSockets you want the server to always report the message "Hello" when it's connected to, so you can test things out more easily? Or even write a whole new website which _only_ makes a WebSocket connection and displays a message?
47+
48+
Once you have an example WebSocket working, and understand how it works, it should be easier for you to apply that to the real problem you're trying to solve.
49+
50+
### Think about the protocol you want
51+
52+
WebSockets let you send arbitrary text (or binary) messages.
53+
54+
In our quote server, we switched from our backend returning a pre-formatted string of a quote, to returning a JSON object so we could get the parts ourselves.
55+
56+
Think about what structure would be useful for our client and our server to know about.
57+
58+
If we're going to add more messages in the future (e.g. for "liking" a message), how will the receiver of the message know what kind of message the one it receives is?
59+
60+
One thing we often do is wrap our message in an object, with a field specifically saying what the command is.
61+
62+
e.g. instead of sending:
63+
```json
64+
{
65+
"user": "Azin",
66+
"message": "Hello!"
67+
}
68+
```
69+
70+
we may send:
71+
72+
```json
73+
{
74+
"command": "send-message",
75+
"message": {
76+
"user": "Azin",
77+
"message": "Hello!"
78+
}
79+
}
80+
```
81+
82+
This means that if we add new commands in the future, we don't need to change our existing code.
83+
84+
### Think about timings
85+
86+
When we first load a page, we need to get all of the messages that already exist.
87+
88+
After that, we can ask to be notified of new messages.
89+
90+
There are a few ways we could do that. An interesting question is what happens _between_ these events?
91+
92+
Imagine we made an HTTP GET request to ask for all of the messages, then created a WebSocket to get new messages. What happens if someone sent a message between when we got our response, and when the WebSocket was connected? How can we make sure we don't miss any messages?
93+
94+
Or imagine we made a WebSocket request, and expected to receive a list of all previous messages, and then to keep receiving updates. Does the server need to remember which messages have already been sent to each client?
95+
96+
{{<note type="Exercise">}}
97+
Write down your strategy for how to make sure that after initially getting the existing messages, your client won't miss any new messages.
98+
{{</note>}}
99+
100+
### Remember WebSockets are bi-directional
101+
102+
Now, we're using a `POST` request to send a new message, and a `WebSocket` to stream receiving new messages.
103+
104+
But we know that WebSockets are bi-directional - we can both send and receive information on them.
105+
106+
We could change our sending logic to also use our WebSocket. Or we could keep using HTTP POST requests. Both of these approaches work.
107+
108+
{{<note type="Exercise">}}
109+
Think: What advantages does each approach have?
110+
111+
Why might we want to change our implementation to use a WebSocket for sending messages?
112+
113+
Why might we want to keep using POST requests for sending messages?
114+
115+
Why might we want to support _both_ on our server? Why might we only want to support one on our server?
116+
{{</note>}}

Diff for: org-cyf-sdc/content/decomposition/sprints/3/prep/index.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ emoji= '🧑🏾‍💻'
66
menu_level = ['sprint']
77
weight = 1
88
[[blocks]]
9+
name = "Websockets"
10+
src = "module/decomposition/websockets"
11+
[[blocks]]
12+
name = "Adding like/dislike"
13+
src = "module/decomposition/adding-like-dislike"
14+
[[blocks]]
915
title = "Different experiences for different users"
1016
src = "module/decomposition/different-experiences-for-different-users"
1117
[[blocks]]

0 commit comments

Comments
 (0)