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

SocketIO session and Express session is not synced #4383

Closed
AlexInCube opened this issue May 28, 2022 · 2 comments
Closed

SocketIO session and Express session is not synced #4383

AlexInCube opened this issue May 28, 2022 · 2 comments
Labels
question Further information is requested

Comments

@AlexInCube
Copy link

AlexInCube commented May 28, 2022

When I change something in a session using express, this data does not appear in the socket io. Even using session.save

Express:

app.get('/getUserGuilds', async (req, res) => {
    if (req.session.user) {
      oauth.getUserGuilds(req.session.user.access_token).then((userGuildsList) => {
        const botGuildsList = []
        userGuildsList.forEach((element) => {
          const guild = client.guilds.cache.get(element.id)// Бот проверяет только те гильдии, в которых он присутствует.

          if (guild?.members.cache.find(user => user.id === client.user.id)) {
            botGuildsList.push(element)
          }
        })
        req.session.guilds = botGuildsList
        req.session.save(() => {
          res.send({ botGuildsList })
        })
      })
    } else {
      res.status(401).send({})
    }
  })

I get socket.request.session without guilds field

SocketIO:

socket.on('joinAudioPlayer', guildId => {
      socket.request.session.reload(function () {})
      socket.rooms.forEach((room) => {
        socket.leave(room)
      })
      const guilds = socket.request.session.guilds
      // console.log(socket.request.session)
      if (guilds) {
        if (guilds.some(guild => guild.id === guildId)) {
          socket.join(guildId)
        }
      }
    })

Edit by @darrachequesne: format

@AlexInCube AlexInCube added the to triage Waiting to be triaged by a member of the team label May 28, 2022
@darrachequesne
Copy link
Member

Hi! I think you need to move the room logic in the socket.request.session.reload() callback:

socket.on('joinAudioPlayer', guildId => {
  socket.request.session.reload(function () {
    socket.rooms.forEach((room) => {
      socket.leave(room)
    })
    const guilds = socket.request.session.guilds
    // console.log(socket.request.session)
    if (guilds) {
      if (guilds.some(guild => guild.id === guildId)) {
        socket.join(guildId)
      }
    }
  })
})

Please see the example here: https://socket.io/how-to/use-with-express-session

@darrachequesne darrachequesne added question Further information is requested and removed to triage Waiting to be triaged by a member of the team labels May 29, 2022
@AlexInCube
Copy link
Author

I'm incredibly stupid, for some reason I completely ignored the callback in reload. Thanks you.

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

No branches or pull requests

2 participants