Skip to content

Commit 4154aaa

Browse files
authored
[OA][Fullstack] - Welcome to Sarge, Websockets ( Node ) (#260)
* feat: ws in node * feat: welcome to sarge, websockets (node)
1 parent 944ba20 commit 4154aaa

7 files changed

Lines changed: 112 additions & 4 deletions

File tree

.gitignore

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ next-env.d.ts
4343
/src/generated/prisma
4444

4545
.vscode
46-
47-
.vercel
48-
.env*.local
46+
47+
.vercel
48+
.env*.local
49+
50+
# ws
51+
/src/ws/node_modules

docker-compose.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ services:
99
DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
1010
depends_on:
1111
- db
12+
- ws
1213
volumes:
1314
- .:/app
1415
- /app/node_modules
@@ -24,6 +25,16 @@ services:
2425
- '5432:5432'
2526
volumes:
2627
- postgres_data:/var/lib/postgresql/data
28+
ws:
29+
container_name: sarge_dev_ws
30+
build:
31+
context: ./src/ws/
32+
dockerfile: Dockerfile
33+
ports:
34+
- 8080:8080
35+
volumes:
36+
- ./src/ws:/src/ws
37+
- /src/ws/node_modules
2738

2839
volumes:
2940
postgres_data:

src/ws/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM node:22-slim
2+
WORKDIR /src/ws
3+
RUN corepack enable pnpm
4+
COPY package.json index.js pnpm-lock.yaml* ./
5+
RUN pnpm install
6+
7+
CMD ["node", "index.js"]

src/ws/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { WebSocketServer } from 'ws';
2+
3+
const ws = new WebSocketServer({ port: 8080 });
4+
5+
ws.on('connection', (ws) => {
6+
ws.on('message', (data) => {
7+
ws.send(data.toString('utf-8'));
8+
});
9+
});

src/ws/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ws",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"type": "module",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "ISC",
13+
"dependencies": {
14+
"ws": "^8.20.0"
15+
}
16+
}

src/ws/pnpm-lock.yaml

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

start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ cleanup() {
88

99
trap cleanup SIGINT SIGTERM
1010

11-
docker compose up -d db && pnpm install && pnpm run dev
11+
docker compose up -d db ws && pnpm install && pnpm run dev

0 commit comments

Comments
 (0)