0

Hi,

I have a socket application with this file structure defined in app.js:

require("../chat/variables");
require("../chat/functions");
require("../chat/connect");

app.get('/', function (req, res, next) {
 res.sendFile(__dirname + '/index.html');
});

httpServer.listen(port, () => {
 console.log(`Socket.IO server running`);
});  

variables file looks like this:

// server
app = require("express")();
httpServer = require("https").createServer({},app);
io = require("socket.io")(httpServer, {      //socket server path
  path: "/"
});

functions file is something like this:

makesession = function(vdata) {   //store user data for this session
    if(socket) {
     socket.userid = vdata.userid;
     socket.username = vdata.name;
    }
   });

the file that performs the connection for each user looks like this:

io.on('connection', (socket) => {   //user connects to socket
   memberdata().then(v => {
    makesession(v);
   });
});   

but Im getting an error at the functions file saying that socket is not defined. Why is that? I thought it was enough with using the if condition so it only triggers after socket is defined.

Thank you.

Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • What exactly is the error message? `ReferenceError: socket is not defined`? – CertainPerformance Dec 29 '22 at 22:29
  • Yes, that one ReferenceError: socket is not defined. And it points to the line: `socket.userid = vdata.userid;` at functions.js – Cain Nuke Dec 29 '22 at 22:31
  • That doesn't make sense. Such an error should definitely arise on the previous line if anywhere. `if(socket) {` – CertainPerformance Dec 29 '22 at 22:32
  • Yes, it doesnt make any sense but thats what it does. If I define my functions inside `io.on('connection', (socket) => {` then no problem but I ant to have an organized file structure so I split it into files. – Cain Nuke Dec 29 '22 at 22:34
  • Could you try removing the two lines: `socket.userid = vdata.userid; socket.username = vdata.name;` and see if the error persists? (I don't believe it; the error should occur as a result of the `if` line, not below) – CertainPerformance Dec 29 '22 at 22:37
  • try adding a `socket` parameter to makesession – jdigital Dec 29 '22 at 22:38
  • sorry, my mistake. Now I get what you mean, The error starts indeed at the if line. Also, I tried adding a socket parameter to makesession but it didnt work either – Cain Nuke Dec 29 '22 at 22:39
  • show your code that added a socket parameter. (it should go without saying but: you'll need to pass in the socket when you invoke makesession) – jdigital Dec 29 '22 at 22:40
  • you mean like this: `makesession(socket,v);` `makesession = function(socket,vdata) {` – Cain Nuke Dec 29 '22 at 22:43

1 Answers1

1

Try adding socket as a parameter to makesession, like this:

makesession = function(socket, vdata) {   //store user data for this session
    if(socket) {
     socket.userid = vdata.userid;
     socket.username = vdata.name;
    }
   });


io.on('connection', (socket) => {   //user connects to socket
   memberdata().then(v => {
    makesession(socket, v);
   });
});

In your original code, socket isn't defined in makesession because socket is a local (not a global) variable in the callback. Javascript uses lexical scoping, not dynamic scoping, so the variable in the callback is not visible in makesession.

jdigital
  • 11,926
  • 4
  • 34
  • 51
  • thanks. This seems to do the trick by now. I am still testing it thoroughly. – Cain Nuke Dec 29 '22 at 22:55
  • is there any way to abbreviate this? I have many other functions besides makesession which means I will be adding the parameter to all of them. Can I cut corners somehow? – Cain Nuke Dec 29 '22 at 22:58
  • to cut corners, you could make `socket` a global variable, but i wouldn't recommend it. – jdigital Dec 29 '22 at 23:15
  • there are other ways to solve this but they would likely require even more change to your code (for example, using a closure or a class). – jdigital Dec 29 '22 at 23:26
  • How would a clossure look like? – Cain Nuke Dec 30 '22 at 01:20
  • Check out this StackOverflow article: https://stackoverflow.com/questions/111102/how-do-javascript-closures-work – jdigital Dec 30 '22 at 05:15
  • Thank you for the reference. I think i get it but I fail to see how to apply that in this case. I know that if I define all my functions inside `io.on('connection', (socket) => {` the problem with socket not being defined would go away but then again, that implies to have all my code in one file instead of separate files as I intend. – Cain Nuke Dec 30 '22 at 18:56
  • help please????? – Cain Nuke Dec 31 '22 at 18:50