Skip to content

Commit

Permalink
feat : Socket with real-time chat
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Dongwook committed Oct 21, 2024
1 parent 74ce495 commit 339f2c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { io, Socket } from "socket.io-client";
const URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:4000";

const socket: Socket = io(URL, {
autoConnect: false,
withCredentials: true,
});

socket.on("connect", () => {
console.log("Connected to the server");
});

socket.on("message", (msg) => {
console.log("Message from server: ", msg);
});

socket.emit("message", "Hello Server!");

export default socket;
14 changes: 14 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
credentials: true,
},
});

Expand All @@ -22,6 +23,19 @@ app.use(express.json());

connectDB();

io.on("connection", (socket) => {
console.log("User connected:", socket.id);

socket.on("disconnect", () => {
console.log("User disconnected");
});

socket.on("message", (msg) => {
console.log("Message from client:", msg);
io.emit("message", msg);
});
});

app.use("/api/auth", authRoutes);
app.use("/api/documents", documentRoutes);

Expand Down

0 comments on commit 339f2c0

Please sign in to comment.