Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socket.io #23

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Socket.io #23

wants to merge 26 commits into from

Conversation

RhythmAgg
Copy link
Collaborator

  • Multi user socket.io connection
  • Authentication middleware changed
  • Socket Messages send on interval of 10 files uploaded

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whys is this file changed. Make no changes in frontend code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change for now

@@ -12,6 +12,7 @@ import { walk } from "walk";
import { map_user_to_bucket, get_user_bucket, remove_user_bucket, map_file_type, file_stats,file_uploaded } from '../Database_queries/queries.js'

const sem = semaphore(100)
let socket = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const sockets = []

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better make a socket handler which handles all the active sockets

Make another file socketManager.js

with these functions

addSocketConnection()
removeSocketConnection()
...etc

Comment on lines 29 to 39
const updateSocket = (usertoken,clientsocket) => {
const entry = socket.findIndex(usersock => usersock.token == usertoken)
console.log(entry)
if(entry != -1)
socket[entry].sock = clientsocket
else
socket.push({token: usertoken,sock: clientsocket})
}
const removeSocket = (index) => {
socket.splice(index,1)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move all to the socketManager.js

@@ -25,6 +26,17 @@ let convert_tiff_options = {
logLevel: 1
};

const updateSocket = (usertoken,clientsocket) => {
const entry = socket.findIndex(usersock => usersock.token == usertoken)
console.log(entry)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Comment on lines 200 to 201
socket[sock].sock.disconnect()
removeSocket(sock)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put all this in disconnectSiocket()

Comment on lines 287 to 290
export {
router,
updateSocket
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any route file should only export router as default

if you want to export anything else, shift it to helpers/ and export from there

import { fileURLToPath } from 'url';
import require_auth from './middleware/Auth.js';
import { Server } from 'socket.io'
const server = http.createServer(app);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this required

Comment on lines +43 to +63
const io = new Server(server, {
cors: {
origin: '*'
},
});

// Add this
// Listen for when the client connects via socket.io-client
io.on('connection', (socket) => {
console.log(`User connected ${socket.id}`);

socket.on('addUser',(user) => {
updateSocket(user,socket)
})

//when disconnect from client
socket.on("disconnect", () => {
console.log("a user disconnected!");
});
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to socketManager

we dont need all this code in app.js which is entry point of Node server

server/server.js Outdated
});
});

server.listen(5000, function () {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user process.env || 5000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants