-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
36 lines (27 loc) · 905 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import express from 'express';
import dotenv from 'dotenv';
import { createServer } from 'http';
import { Server as WebSocketServer } from 'socket.io';
dotenv.config();
import { handler } from "./build/handler.js";
const port = process.env.PORT || 3000;
const app = express();
const server = createServer(app);
const io = new WebSocketServer(server);
io.on("connection", (socket) => {
socket.on("join", (chatId) => socket.join(chatId));
socket.on("message", async (message) => {
io.to(message.chatId).emit("message", message);
});
socket.on("deleteMessage", async (messageId, chatId) => {
io.to(chatId).emit("deleteMessage", messageId, chatId);
});
});
/*
* SvelteKit should handle everything else using Express middleware
* https://kit.svelte.dev/docs/adapter-node#custom-server
*/
app.use(handler);
server.listen(port, () => {
console.log("Listening on port", port);
});