Manish Chandra
Manish Chandra

Reputation: 27

Socket.io and local storage

I have made a chat app using socket.io and node.

const LocalStorage = require('node-localstorage').LocalStorage let localStorage = new LocalStorage('./scratch') localStorage.setItem('name_test', 'tom') console.log(localStorage.getItem('name_test'))

Any suggestion , as how to tackle the scenario / issue is most appreciated on this ?

Upvotes: 1

Views: 3949

Answers (1)

Jackthomson
Jackthomson

Reputation: 644

When you fire the disconnect event, you could set your local storage just before. If I am understanding correctly I believe the below example should work for you

function disconnectUserFromChat() {
  return new Promise(function(resolve, reject) {
    try {
      localStorage.setItem('key', 'Value')
      resolve()
    } catch(error) {
      reject(error)
    }
  })
})

disconnectUserFromChat()
  .then(function() {
    socket.on('disconnect', function() {
      console.log('Disconnected')
    })
  })

Upvotes: 1

Related Questions