-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49ac6c3
commit 878ba41
Showing
37 changed files
with
4,646 additions
and
19,944 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "backend", | ||
"version": "1.0.0", | ||
"description": "chat room app backend", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "sankhadip roy", | ||
"license": "ISC", | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"express": "^4.19.2", | ||
"socket.io": "^4.7.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const express = require('express') | ||
const app = express(); | ||
const http = require("http"); | ||
const cors = require('cors'); | ||
const { Server } = require("socket.io"); | ||
|
||
app.use(cors()); | ||
|
||
const server = http.createServer(app); | ||
const io = new Server(server, { | ||
cors: { | ||
origin: 'http://localhost:5173', | ||
methods: ['GET', 'POST'], | ||
}, | ||
}); | ||
|
||
io.on("connection", (socket) => { | ||
console.log(`User connected ${socket.id}`); | ||
|
||
// Handle 'send_message' event | ||
socket.on('send_message', (data) => { | ||
console.log(`Received message: ${data.message}`); | ||
// Broadcast the message to all connected clients | ||
io.emit('recive_message', data); | ||
}); | ||
|
||
socket.on('disconnect', () => { | ||
console.log('User disconnected'); | ||
}); | ||
}); | ||
|
||
server.listen(3001, () => { | ||
console.log(`Example app listening on port 3001`); | ||
}); |
Oops, something went wrong.