L
L
LemanRass092017-01-17 14:41:11
Node.js
LemanRass09, 2017-01-17 14:41:11

Express + socket.io how to bundle?

Hello.
I am making a website using node.js with express and socket.io packages.
Something like this:

app.get('/rules', function(req, res)
{
  fs.readFile("C:/Apache24/htdocs/site/rules.html", "utf-8", function(err, data)
  {
    if(err) throw err;
    res.send(data);
  });
});

and so on for each page.
I also have socket.io on the server, so far only in this form:
io.on('connection', function (socket) 
{
  console.log("New user connected!");	
  
  socket.on('disconnect', function() 
  {
      console.log('User disconnected!');
   });
});

I needed to add authorization through steam, and I did it using this package.
Now the question itself.
Authorization through steam occurs strictly through express, while socket.io is not involved at all there.
After authorization, wherever the user goes, information about him can always be found in the first parameter of the callback function from the express router in the form: req.user. But socket io knows nothing about it and works separately.
I would like to associate a socket.io socket with a user id (the rest of the information about it will be stored in the database) as an associative array so that by the user id I can safely send packets to him through socket.io. But I don't know how to do it because socket.io and express don't intersect at all in my application. Express knows everything about the user who is logged in, but does not know his socket.io socket. Socket.io knows the user's socket but has no idea which user is the owner of that socket.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nwton, 2017-01-17
@LemanRass09

Faced the task of authorizing steam on node js + socket.
The essence of steam-login itself is to make sure that the user who provided you with data about the steam account is indeed the owner of it. Ie, steam authorization is primarily a check. After authorization through steam-login, you get the login of the user's steam account. From him and dance.
1) The user is authorized through steam-login. You get the user's login.
2) Create a cookie and bind it to login, add it to the database. Also set this cookie in the user's browser.
3) With socket connect, you get the browser cookies, look for the login in the database by the cookies, and that's it. Now you know which user is sending socket requests.
p.s. I used passport-steam instead of steam-login

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question