2

I'm storing user sessions in mongo collection, using express-session middleware.

Which is the best way, to update all user sessions, when change performed from one of them.

For example, if user changes email from session A, I'm updating it instantly. But changes don't apply to the rest.

For clarity: Browser 1 - User changed email Browser 2 - Reload page, still getting old data

app.get('/change-email', async (req, res, next) => {
  const userData = await User.findOneAndUpdate({ _id: req.session.user.id }, {
    $set: {
      email: req.body.email,
    },
  }, {
    new: true,
  });
  req.session.user = {
    id: userData._id,
    email: userData.email,
    username: userData.username,
    name: userData.name,
  };
});

I expect the change performed from session A, will be distributed to the rest of the user sessions.

As one of the solutions to the problem, I think to search for all user sessions and update them on every user data (email, username, name, etc.) change.

Daniel R.
  • 21
  • 1

1 Answers1

0

You can use EventSource() at the client to stream data to the browser from the server.

At the server send one or more messages to the clients having MIME type "text/event-stream". For example, see How to read and echo file size of uploaded file being written at server in real time without blocking at both server and client?.

guest271314
  • 1
  • 15
  • 104
  • 177
  • I have simple multi page app, using express views and trying to use more conservative and the same time effective way. – Daniel R. Feb 09 '19 at 23:43
  • @DanielR. At `load` event of each HTML `document` you can create a new `EventSource` instance and send query string parameters which include the client unique identifier to the server with the `GET` request. The server checks if any relevant data has been updated and streams messages to the clients. When any HTML `document` is reloaded a new `EventSource` instance is created which streams the current data to the client. Not sure what you mean by _"conservative"_. Since communication is only needed from server to client the server performs the task of serving the continuous stream of data. – guest271314 Feb 09 '19 at 23:47
  • @DanielR. Alternative approaches include using dedicated `Worker`s or `SharedWorker`s [How can I load a shared web worker with a user-script?](https://stackoverflow.com/questions/38810002/); [Can we refer to JavaScript variables across webpages in a browser session?](https://stackoverflow.com/questions/36146595/can-we-refer-to-javascript-variables-across-webpages-in-a-browser-session), to the extent `Worker`s would achieve the expected result, or a `ServiceWorker` and [Push Noticiations](https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications) – guest271314 Feb 09 '19 at 23:57
  • Thanks for very detailed explanation. I'm trying now to implement `EventSource`, to stream data, as you suggest. Will reply as soon as I get result. – Daniel R. Feb 10 '19 at 00:02
  • @DanielR. See [Server Sent Event / EventSource with node.js (express)](https://stackoverflow.com/questions/31700336/server-sent-event-eventsource-with-node-js-express); [How to use server-sent-events in express.js](https://stackoverflow.com/questions/34657222/how-to-use-server-sent-events-in-express-js) – guest271314 Feb 10 '19 at 00:05