How to push data from server to all clients not using Collections?

前端 未结 2 2043
盖世英雄少女心
盖世英雄少女心 2021-02-10 01:07

I need to inform clients about changes on server side. In my case I am using different Collections on server and on client (more about it in this question: how would you build p

相关标签:
2条回答
  • 2021-02-10 01:52

    To accomplish what you want you do need another collection - on the client. On the server, in a publish function, build a document from scratch assigning the current count of Products to an attribute. Using observe() and set, modify count when documents are added or removed from Products. Subscribe to the count "record set" on the client.

    // Server
    Meteor.publish('count', function () {
        // Build a document from scratch
        var self = this;
        var uuid = Meteor.uuid();
        var count = Products.find().count();
        // Assign initial Products count to document attribute
        self.set('count', uuid, {count: count});
    
        // Observe Products for additions and removals
        var handle = Products.find().observe({
            added: function (doc, idx) {
                count++;
                self.set('counts', uuid, {count: count});
                self.flush();
            },
            removed: function (doc, idx) {
                count--;
                self.set('counts', uuid, {count: count});
                self.flush();
            }
        });
        self.complete();
        self.flush();
        self.onStop(function () {
            handle.stop();
        });
    });
    
    // Client
    Counts = new Meteor.Collection('count');
    Meteor.subscribe('count');
    console.log('Count: ' + Counts.findOne().count);
    
    0 讨论(0)
  • 2021-02-10 01:56

    I must say the above solution showed me one way, but still, what if I need to publish to client data that are not connected with observe()? Or with any collection?

    In my case I have i.e. 1000 products. To engage visitors I am "refreshig" the collection by updating the timestamp of random number of products, and displaying collection sorted by timestamp. Thank to this visitors have impression that something is happening.

    My refresh method returns number of products (it is random). I need to pass that number to all clients. I did it, but using (I think) ugly workaround.

    My refresh method sets Session.set('lastRandomNo', random). BTW: I didn't know that Session works on server side. refresh updates Products collection.

    Then accoriding to above answer:

    Meteor.publish 'refreshedProducts', ->
    
    self = this
    uuid = Meteor.uuid()
    
    # create a new collection to pass ProductsMeta data
    self.set('products_meta', uuid, { refreshedNo: 0 })
    
    handle = Products.find().observe
      changed: (newDocument, atIndex, oldDocument) ->
        self.set('products_meta', uuid, { refreshedNo: Session.get('lastRandomNo') })
        self.flush()
    
    self.complete()
    self.flush()
    self.onStop ->
      handle.stop()
    

    and on client side:

    ProductsMeta = new Meteor.Collection('products_meta')
    
    # subscribe to server 'products_meta' collection that is generated by server
    Meteor.subscribe('refreshedProducts')
    
    ProductsMeta.find({}).observe
      changed: (newDocument, atIndex, oldDocument) ->
    
        # I have access to refreshedNo by
        console.log ProductsMeta.findOne().refreshedNo
    

    What do you think?

    0 讨论(0)
提交回复
热议问题