samedi 30 juillet 2016

Socket.io create a separate event handler file

I have crated a socket connection, and at the moment I am looking to just clean up and make everything well organized.

Here is a image of my current file structure FILE STRUCTURE

So what I am wanting to do is take

socket.on("send message", function(data){
    io.sockets.emit("new message", data);
});

and put it in the file called socketEvents.js which is located in main --> js.

However I am not 100% sure on how to include that file on successful connection. I have tried using something like require(); but to no avail. Is there a standard method of including a separate file to run events? Or is it not good practice?

Aucun commentaire:

Enregistrer un commentaire