Skip to content

Commit

Permalink
Socket backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Keagan Aromin authored and Keagan Aromin committed Jan 12, 2025
1 parent bbd747c commit 15b618c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import messageRoutes from "./routes/message.routes.js";
import userRoutes from "./routes/user.routes.js";

import connectToMongoDB from "./db/connectToMongoDB.js";

const app = express();
import { app, server } from "./socket/socket.js";
//const app = express(); moved to socket.js as a lower layer
const PORT = process.env.PORT || 5000;

dotenv.config();
Expand All @@ -27,7 +27,7 @@ app.use("/api/users", userRoutes);
// res.send("Hello World!!");
// });

app.listen(PORT, () => {
server.listen(PORT, () => {
connectToMongoDB();
console.log(`Server Running on port ${PORT}`)
});
Expand Down
22 changes: 22 additions & 0 deletions backend/socket/socket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Server} from 'socket.io';
import http from 'http';
import express from 'express';

const app = express();
const server = http.createServer(app);
const io = new Server(server, {
cors:{
origin:['http://localhost:3000'],
methods:['GET','POST']
}
})

io.on('connection', (socket) => {
console.log("user has connected", socket.id);

//socket.on is a listener for the events; can be used on both client and server side
socket.on('disconnect', () => {
console.log('user disconnected', socket.id)
})
})
export {app, io, server}
1 change: 1 addition & 0 deletions frontend/src/components/sidebar/SearchInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const SearchInput = () => {

if (conversation) {
setSelectedConversation(conversation)
setSearch('asd')
} else toast.error("No such user found")
};
return (
Expand Down

0 comments on commit 15b618c

Please sign in to comment.