diff --git a/client/src/lib/socket.ts b/client/src/lib/socket.ts index cd0f5dd..1075765 100644 --- a/client/src/lib/socket.ts +++ b/client/src/lib/socket.ts @@ -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; diff --git a/server/src/app.ts b/server/src/app.ts index 1ccc8c9..82613b1 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -14,6 +14,7 @@ const io = new Server(server, { cors: { origin: "*", methods: ["GET", "POST"], + credentials: true, }, }); @@ -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);