Skip to content

Commit 6c7a349

Browse files
Merge pull request #692 from aXenDeveloper/ws
feat: Add websockets
2 parents e470001 + 0e3236c commit 6c7a349

31 files changed

Lines changed: 1469 additions & 19 deletions

apps/api/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"react": "^19.2.5",
2525
"react-dom": "^19.2.5",
2626
"use-intl": "^4.11.0",
27+
"ws": "^8.21.0",
2728
"zod": "^4.4.1"
2829
},
2930
"devDependencies": {
@@ -32,8 +33,10 @@
3233
"@types/node": "^25.6.0",
3334
"@types/react": "^19.2.14",
3435
"@types/react-dom": "^19.2.3",
36+
"@types/ws": "^8.18.1",
3537
"@vitnode/config": "workspace:*",
3638
"@vitnode/nodemailer": "workspace:*",
39+
"@vitnode/blog": "workspace:*",
3740
"dotenv": "^17.4.2",
3841
"eslint": "^10.2.1",
3942
"react-email": "^6.1.5",

apps/api/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { serve } from "@hono/node-server";
1+
import { serve, upgradeWebSocket } from "@hono/node-server";
22
import { OpenAPIHono } from "@hono/zod-openapi";
33
import { VitNodeAPI } from "@vitnode/core/api/config";
4+
import { handleVitNodeWebSocket } from "@vitnode/core/ws/handle";
5+
import { WebSocketServer } from "ws";
46

57
import { vitNodeApiConfig } from "./vitnode.api.config.js";
68

@@ -11,10 +13,17 @@ VitNodeAPI({
1113
vitNodeApiConfig,
1214
});
1315

16+
const wss = new WebSocketServer({ noServer: true });
17+
18+
app.get("/ws", upgradeWebSocket(handleVitNodeWebSocket()));
19+
1420
serve(
1521
{
1622
fetch: app.fetch,
1723
port: 8080,
24+
websocket: {
25+
server: wss,
26+
},
1827
},
1928
info => {
2029
const initMessage = "\x1b[34m[VitNode]\x1b[0m";

apps/api/src/vitnode.api.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { blogApiPlugin } from "@vitnode/blog/config.api";
12
import { buildApiConfig } from "@vitnode/core/vitnode.config";
23
import { NodemailerEmailAdapter } from "@vitnode/nodemailer";
34
import { config } from "dotenv";
@@ -11,7 +12,7 @@ export const POSTGRES_URL =
1112
process.env.POSTGRES_URL ?? "postgresql://root:root@localhost:5432/vitnode";
1213

1314
export const vitNodeApiConfig = buildApiConfig({
14-
plugins: [],
15+
plugins: [blogApiPlugin()],
1516
pathToMessages: async path => await import(`./locales/${path}`),
1617
dbProvider: drizzle({
1718
connection: POSTGRES_URL,

apps/docs/content/docs/dev/meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"email",
2020
"sso",
2121
"cron",
22+
"websocket",
2223
"---Frontend---",
2324
"layouts-and-pages",
2425
"admin-page",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Redis
3+
description: Enhance your application's performance and scalability.
4+
---
5+
6+
<Callout type="warn" title="This documentation is under construction! 🚧">
7+
We're working hard to bring you the best documentation experience.
8+
</Callout>
9+
10+
## Going to production - websockets
11+
12+
The connection list lives in memory on a single server. If you run **several
13+
server instances** behind a load balancer, a `broadcast` or `sendToUser` only
14+
reaches the clients on the same instance. To fix that, share the messages
15+
between instances with a pub/sub like **Redis**.

0 commit comments

Comments
 (0)