Skip to content

Commit

Permalink
fix: fixed websockets by using db collection for active users
Browse files Browse the repository at this point in the history
  • Loading branch information
tiller1010 committed Apr 19, 2024
1 parent 03ab83b commit 315b743
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 45 deletions.
33 changes: 33 additions & 0 deletions database/methods/socketUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { getDB } = require('../db.js');

const addSocketUser = async (socketID, userID) => {
const db = getDB();
const user = await getSocketUserByUserID(userID);
if (user) return;
const result = await db.collection('socketUsers').insertOne({ socketID, userID });
return result;
}

const getSocketUserByUserID = async (userID) => {
const db = getDB();
const user = await db.collection('socketUsers').findOne({ userID });
return user;
}

const deleteSocketUser = async (socketID) => {
const db = getDB();
const user = await db.collection('socketUsers').findOne({ socketID });
const userID = user ? user.userID : null;
const result = await db.collection('socketUsers').deleteOne({ socketID });
if (userID) {
await db.collection('socketUsers').deleteMany({ userID });
}
return result;
}

module.exports = {
addSocketUser,
getSocketUserByUserID ,
deleteSocketUser,
};

14 changes: 7 additions & 7 deletions js/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,19 @@ try {
try {
var socketHost = `https://localhost:${process.env.APP_PORT}`;
} catch(e) {
var socketHost = 'https://localhost:3000';
var socketHost = 'http://localhost:3000';
}
}
window.socket = io(socketHost);
fetch('/current-user-id')
.then(res => res.json())
.then(socketUserID => {
window.socket.emit('Hello Server', socketUserID)
window.socket.on('Hello Client', (...args) => {
console.log('Hello Client')
});
.then(userID => {
window.socket.emit('Hello Server', userID)
// window.socket.on('Hello Client', (...args) => {
// console.log('Hello Client')
// });
window.socket.on('Call Incoming', ({ content, from }) => {
console.log('Call Incoming From server')
// console.log('Call Incoming From server')
var callModal = document.createElement('div');
callModal = document.body.appendChild(callModal);
ReactDOM.render(<NotificationModal buttonAnchor={`call-modal-from-${from}`} modalTitle={'You have an incoming call!'} modalContent={content} />, callModal);
Expand Down
14 changes: 7 additions & 7 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45082,21 +45082,21 @@ try {
try {
var socketHost = `https://localhost:${process.env.APP_PORT}`;
} catch (e) {
var socketHost = 'https://localhost:3000';
var socketHost = 'http://localhost:3000';
}
}

window.socket = (0,socket_io_client__WEBPACK_IMPORTED_MODULE_23__.io)(socketHost);
fetch('/current-user-id').then(res => res.json()).then(socketUserID => {
window.socket.emit('Hello Server', socketUserID);
window.socket.on('Hello Client', (...args) => {
console.log('Hello Client');
});
fetch('/current-user-id').then(res => res.json()).then(userID => {
window.socket.emit('Hello Server', userID); // window.socket.on('Hello Client', (...args) => {
// console.log('Hello Client')
// });

window.socket.on('Call Incoming', ({
content,
from
}) => {
console.log('Call Incoming From server');
// console.log('Call Incoming From server')
var callModal = document.createElement('div');
callModal = document.body.appendChild(callModal);
react_dom__WEBPACK_IMPORTED_MODULE_1__.render( /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__.createElement(_components_NotificationModal_js__WEBPACK_IMPORTED_MODULE_15__["default"], {
Expand Down
2 changes: 1 addition & 1 deletion public/js/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports.defineUserRoutes = function(app) {

// Get authenticated user ID
app.get('/current-user-id', (req, res) => {
if(req.user){
if (req.user) {
return res.json(req.user._id);
}
res.sendStatus(404);
Expand Down
46 changes: 17 additions & 29 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const { defineStripeRoutes } = require('./routes/stripe.js');
const { defineReportABugRoutes } = require('./routes/report-a-bug.js');
const { defineBecomeVerifiedRoutes } = require('./routes/become-verified.js');

// Database
const {
addSocketUser,
getSocketUserByUserID ,
deleteSocketUser,
} = require('./database/methods/socketUsers.js');

// Configure Server
const app = express();
app.set('views', __dirname + '/views');
Expand Down Expand Up @@ -114,43 +121,24 @@ app.use(express.json());
});

const io = socketio(server);
let socketUsers = [];
io.on('connection', (socket) => {

socket.emit('Hello Client');
// socket.emit('Hello Client');

socket.on('disconnect', () => {
let newSocketUsers = [];
for (let user of socketUsers) {
if (!user.socketID == socket.id) {
newSocketUsers.push(user);
}
}
socketUsers = newSocketUsers;
deleteSocketUser(socket.id);
});

socket.on('Hello Server', (userID) => {
console.log('Hello Server');
for (let user of socketUsers) {
if (user.userID == userID) {
user.socketID = socket.id;
return;
}
}
socketUsers.push({
socketID: socket.id,
userID,
});
socket.on('Hello Server', async (userID) => {
const user = await getSocketUserByUserID(userID);
if (user) return;
addSocketUser(socket.id, userID);
});

socket.on('Call Sent', (forUserID, content) => {
console.log('Server received call.');
// console.log(socketUsers)
for (let user of socketUsers) {
if (user.userID == forUserID) {
socket.to(user.socketID).emit('Call Incoming', { content, from: socket.id });
}
}
socket.on('Call Sent', async (forUserID, content) => {
// console.log('Server received call.');
const user = await getSocketUserByUserID(forUserID);
socket.to(user.socketID).emit('Call Incoming', { content, from: socket.id });
});

});
Expand Down

0 comments on commit 315b743

Please sign in to comment.