Skip to content

Commit 20ae795

Browse files
committed
chore: working on chapter 13
1 parent df5a876 commit 20ae795

File tree

15 files changed

+1791
-0
lines changed

15 files changed

+1791
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 01-pubsub-basic-chat
2+
3+
This sample demonstrates the Pub/Sub pattern by implementing a basic chat
4+
application using WebSockets.
5+
6+
## Dependencies
7+
8+
This example requires you to install some third-party dependencies from npm.
9+
10+
If you have `pnpm` installed, you can do that with:
11+
12+
```bash
13+
pnpm install
14+
```
15+
16+
Alternatively, if you prefer to use another package manager, make sure to delete
17+
the `pnpm-lock.yaml` file before using it.
18+
19+
If you want to use `npm`, you can run:
20+
21+
```bash
22+
npm install
23+
```
24+
25+
If you want to use `yarn`, you can run:
26+
27+
```bash
28+
yarn install
29+
```
30+
31+
## Run
32+
33+
To run the example server:
34+
35+
```bash
36+
node index.js <port>
37+
```
38+
39+
For example, to run the server on port 8080:
40+
41+
```bash
42+
node index.js 8080
43+
```
44+
45+
Then you can access the chat application by opening your browser and navigating
46+
to:
47+
48+
```
49+
http://localhost:8080
50+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createServer } from 'node:http'
2+
import staticHandler from 'serve-handler' // v6.1.6
3+
import { WebSocketServer } from 'ws' // v8.18.2
4+
5+
// serve static files
6+
const server = createServer((req, res) => {
7+
return staticHandler(req, res, { public: 'web' })
8+
})
9+
10+
const wss = new WebSocketServer({ server })
11+
wss.on('connection', client => {
12+
console.log('Client connected')
13+
client.on('message', msg => {
14+
console.log(`Message: ${msg}`)
15+
broadcast(msg)
16+
})
17+
})
18+
19+
function broadcast(msg) {
20+
for (const client of wss.clients) {
21+
if (client.readyState === WebSocket.OPEN) {
22+
client.send(msg)
23+
}
24+
}
25+
}
26+
27+
server.listen(process.argv[2] || 8080)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "01-pubsub-basic-chat",
3+
"version": "1.0.0",
4+
"description": "This sample demonstrates the Pub/Sub pattern by implementing a basic chat application using WebSockets.",
5+
"type": "module",
6+
"scripts": {},
7+
"engines": {
8+
"node": ">=24"
9+
},
10+
"engineStrict": true,
11+
"keywords": [],
12+
"author": "Luciano Mammino and Mario Casciaro",
13+
"license": "MIT",
14+
"dependencies": {
15+
"serve-handler": "^6.1.6",
16+
"ws": "^8.18.2"
17+
}
18+
}

13-messaging-and-integration-patterns/01-pubsub-basic-chat/pnpm-lock.yaml

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)