Skip to content

Commit e74df01

Browse files
aster-voidnakaterm
authored andcommitted
feat: use SSE instead of ws (#667)
1 parent a32bb6d commit e74df01

File tree

15 files changed

+207
-213
lines changed

15 files changed

+207
-213
lines changed

bun.lock

Lines changed: 1 addition & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// common/type/types.ts
22

3+
import type { Message } from "./zod/types.ts";
4+
35
export type {
46
UserID,
57
GUID,
@@ -34,3 +36,23 @@ export type {
3436
InitSharedRoom,
3537
UpdateRoom,
3638
} from "./zod/types.ts";
39+
40+
export type SSEChatEvents = {
41+
"Chat:Append": {
42+
message: Message;
43+
sender: string; // user name
44+
};
45+
"Chat:Update": {
46+
id: number;
47+
message: Message;
48+
};
49+
"Chat:Delete": {
50+
id: number;
51+
};
52+
"Chat:Ping": "";
53+
};
54+
export type SSEChatEventEnum = "Chat:Append" | "Chat:Update" | "Chat:Delete";
55+
export type SSEChatEvent<T extends SSEChatEventEnum> = {
56+
event: T;
57+
data: SSEChatEvents[T];
58+
};

package.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
{
22
"name": "course-mate",
33
"version": "1.0.0",
4-
"description": "",
4+
"author": "",
55
"main": "index.js",
6+
"devDependencies": {
7+
"@biomejs/biome": "^1.9.1",
8+
"lefthook": "^1.10.10",
9+
"lint-staged": "^15.2.10"
10+
},
11+
"description": "",
12+
"keywords": [],
13+
"license": "ISC",
614
"scripts": {
715
"prepare": "lefthook install && (cd server; bun run prepare)",
816
"check": "bun type && bun style:check",
@@ -27,15 +35,6 @@
2735
"test": "make test",
2836
"spell": "bunx cspell --quiet ."
2937
},
30-
"keywords": [],
31-
"author": "",
32-
"license": "ISC",
33-
"workspaces": ["common", "web", "server"],
34-
"devDependencies": {
35-
"@biomejs/biome": "^1.9.1",
36-
"lefthook": "^1.10.10",
37-
"lint-staged": "^15.2.10"
38-
},
39-
"dependencies": {},
40-
"trustedDependencies": ["@biomejs/biome", "lefthook"]
38+
"trustedDependencies": ["@biomejs/biome", "lefthook"],
39+
"workspaces": ["common", "web", "server"]
4140
}

server/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "src/index.ts",
66
"scripts": {
77
"prepare": "bun --env-file=./.env.dev prisma generate --sql || (echo 'please set DATABASE_URL in server/.env.dev' && exit 1)",
8-
"dev": "bun --watch src/main.ts",
8+
"dev": "bun --watch src/index.ts",
99
"build": "tsc",
1010
"serve": "bun target/main.js"
1111
},
@@ -24,10 +24,9 @@
2424
"dotenv": "^16.4.5",
2525
"dotenv-cli": "^7.4.2",
2626
"firebase-admin": "^12.2.0",
27-
"lru-cache": "^11.0.2",
2827
"hono": "^4.7.1",
28+
"lru-cache": "^11.0.2",
2929
"sharp": "^0.33.5",
30-
"socket.io": "^4.7.5",
3130
"sql-formatter": "^15.4.10",
3231
"zod": "^3.23.8"
3332
},

server/src/functions/chat.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
SendMessage,
99
ShareRoomID,
1010
} from "common/types";
11+
import { HTTPException } from "hono/http-exception";
1112
import * as db from "../database/chat";
1213
import { areAllMatched, getRelation } from "../database/matches";
1314
import { getUserByID } from "../database/users";
@@ -37,12 +38,13 @@ export async function sendDM(
3738
from: UserID,
3839
to: UserID,
3940
send: SendMessage,
40-
): Promise<http.Response<Message>> {
41+
): Promise<Message> {
4142
const rel = await getRelation(from, to);
4243
if (rel.status === "REJECTED")
43-
return http.forbidden(
44-
"You cannot send a message because the friendship request was rejected.",
45-
);
44+
throw new HTTPException(403, {
45+
message:
46+
"You cannot send a message because the friendship request was rejected.",
47+
});
4648

4749
// they are now MATCHED
4850
const msg: Omit<Omit<Message, "id">, "isPicture"> = {
@@ -53,8 +55,9 @@ export async function sendDM(
5355
};
5456

5557
const result = await db.sendDM(rel.id, msg);
56-
if (!result) return http.internalError("Failed to send DM");
57-
return http.created(result);
58+
if (!result)
59+
throw new HTTPException(500, { message: "Failed to send message" });
60+
return result;
5861
}
5962

6063
export async function getDM(

server/src/index.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import type { Server } from "node:http";
21
import { Hono } from "hono";
32
import { cors } from "hono/cors";
43
import { HTTPException } from "hono/http-exception";
5-
import { initializeSocket } from "./lib/socket/socket";
64
import { allUrlMustBeValid, env } from "./lib/utils";
75
import chatRoutes from "./router/chat";
86
import coursesRoutes from "./router/courses";
97
import matchesRoutes from "./router/matches";
108
import pictureRoutes from "./router/picture";
119
import requestsRoutes from "./router/requests";
10+
import sseRoutes from "./router/sse";
1211
import subjectsRoutes from "./router/subjects";
1312
import usersRoutes from "./router/users";
1413

@@ -31,12 +30,11 @@ if (corsOptions.origin.length > 1) {
3130
const app = new Hono()
3231
.onError((err, c) => {
3332
if (err instanceof HTTPException) {
34-
console.error(err);
35-
c.status(err.status);
36-
return c.json({ error: err });
33+
console.log(err);
34+
return c.json({ error: err.message }, err.status);
3735
}
38-
c.status(500);
39-
return c.json({ error: err });
36+
console.error(err);
37+
return c.json({ error: err }, 500);
4038
})
4139

4240
.use(cors(corsOptions))
@@ -51,15 +49,7 @@ const app = new Hono()
5149
.route("/subjects", subjectsRoutes)
5250
.route("/requests", requestsRoutes)
5351
.route("/matches", matchesRoutes)
54-
.route("/chat", chatRoutes);
52+
.route("/chat", chatRoutes)
53+
.route("/sse", sseRoutes);
5554

56-
export function main() {
57-
const server = Bun.serve({
58-
fetch: app.fetch,
59-
port: process.env.PORT ?? 3000,
60-
});
61-
// ??
62-
initializeSocket(server as unknown as Server, corsOptions);
63-
return server;
64-
}
6555
export default app;

server/src/lib/socket/socket.ts

Lines changed: 0 additions & 76 deletions
This file was deleted.

server/src/main.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)