Sever-client data exchange without writing on disk?

Hi there!

The server generates some statuses. What is the best way to pass them to the client?

I would imagine that creating a memory only collection would be a best choice, but didn’t find how to accomplish this in Meteor?

It would help us if you gave us a bit more info… you need this to be reactive? Instantaneous?
I use a normal collection for something comparable. The client who needs to know monitors for inserts on this collection. If a new one is recieved, it will read it and delete it. This works extreemly fast and quit well for my purposes. (I can give multiple, different commands or updates to the client this way!)
Of course, if you have multiple updates a second, or 100.000 clients, or… it could differ :slight_smile:

1 Like

Thank you for opinion. No need for instantaneous feedbacks - the server updates statuses every 30 seconds anyway. And number of active clients would be just few. I think I would implement the same approach, but I am a bit surprised that there is no option for memory only collection…

There’s a couple of ways you could implement a memory only option.

The first, using pub/sub, is to use the low-level added / changed / removed DDP messages in a publish function. Here’s a quick example tracking online status of users:

// Server
Meteor.publish('onlineUsers', function () {
    const onlineUsers = [];
    Accounts.onLogin(details => {
        if (onlineUsers.includes(details.user._id)) return
        
        onlineUsers.push(details.user._id)
        this.added('onlineUsers', details.user._id, {online: true})
    });
    Accounts.onLogout(details => {
        if (!onlineUsers.includes(details.user._id)) return
        
        onlineUsers.splice(onlineUsers.indexOf(details.user._id))
        this.removed('onlineUsers', details.user._id)
    });
    this.ready()
});
// Client
const onlineUsersCollection = new Mongo.Collection('onlineUsers');
Meteor.subscribe('onlineUsers', function onReady() {
    const currentlyOnline = onlineUsersCollection.find().fetch()
});

Docs here: https://docs.meteor.com/api/pubsub.html

Second, you can use a library that allows you to send your own messages across the websocket (like meteor-streams or streamy and DIY the tracking

Third, you can use meteor methods (or HTTP) and polling, push the data onto an array on the server and send a copy of the array each time you get a request:

const onlineUsers = [];
Accounts.onLogin(details => {
  onlineUsers.push(details.user._id);
});
Accounts.onLogout(details => {
  onlineUsers.splice(onlineUsers.indexOf(details.user._id));
});
Meteor.methods({
  getOnlineUsers() {
    return onlineUsers;
  },
});
1 Like

Thanks every one for suggestion!.
Right now I implemented standard collection. Will see if the performance would be OK or not.

Kind regards,
Alex