Confused about Meteor: how to send data to all clients without writing to the database?

Actually, I played a little with Meteor, but I realized that I still lack understanding (or a lot!) On this topic.

For example, here is a tutorial that uses node.js / express / socket.io to make a simple real-time chat: http://net.tutsplus.com/tutorials/javascript-ajax/real-time-chat-with-nodejs -socket-io-and-expressjs /

In the above example, through socket.io, the web server receives some data and transfers it to all connected clients - all without access to the database.

In Meteor, in all the examples I saw, clients are updated by writing mongodb, which then updates all clients. But what if I do not need to write data to the database? This seems like an expensive step to transfer data to all clients.

I'm sure something is missing here. What will be the Meteor method for updating all clients (say, using a simple chat application), but without the cost of writing to the database?

Thanks!

+8
meteor
source share
4 answers

There is currently no official way to send data to customers without writing to the collection. Its a little trick to a meteor, because the step of sending data to several clients, when there is no space for recording, comes from the fact that several meteors are used together. I. Items sent from one meteor will not arrive at customers signed to another.

There is a workaround using Meteor Streams ( http://meteorhacks.com/introducing-meteor-streams.html ) that allows you to do what you want without writing to the database in the meantime.

There is also a fairly extensive discussion about this on meteorites ( https://groups.google.com/forum/#!topic/meteor-talk/Ze9U9lEozzE ), if you want to understand some technical details, it will really become possible when the linker branch is merged into master, for one server

Here are some ways to have a "virtual collection that is not perfect, but it can do until Meteor has a more polished way of doing it."

Meteor.publish("virtual_collection", function() { this.added("virtual_coll", "some_id_of_doc", {key: "value"}); //When done this.ready() }); 

Then subscribe to this on the client:

 var Virt_Collection = new Meteor.Collection("virtual_coll"); Meteor.subscribe("virtual_collection"); 

Then you can run this when the subscription is complete:

 Virt_Collection.findOne(); => { _id: "some_id_of_doc", key: "value"} 

It's a little dirty, but you can also connect to it to update or delete collections. At least this way, although you will not use any plugins or packages.

See: https://www.eventedmind.com/posts/meteor-how-to-publish-to-a-client-only-collection for more details and a video example.

+10
source share

The publish function on the server sends data to clients. It has some convenient shortcuts for publishing query results from a database, but you do not need to use them. The publish function has this.added (), this.removed (), and this.changed (), which let you publish whatever you choose. Then the client subscribes and receives the published data.

For example:

 if ( Meteor.isClient ){ var someMessages = new Meteor.Collection( "messages" ); //messages is name of collection on client side Meteor.subscribe ( "messagesSub" ); //messagesSub tells the server which publish function to request data from Deps.autorun( function(){ var message = someMessages.findOne({}); if ( message ) console.log( message.m ); // prints This is not from a database }); } if (Meteor.isServer ) { Meteor.publish( "messagesSub", function(){ var self = this; self.added ( "messages", "madeUpId1", { m: "This is not from a database"} ); //messages is the collection that will be published to self.ready(); }); } 

The meteor docs document provides an example here and another example here . I also have an example that shares data between clients, never using a database to teach myself how publishing and subscribing works. Nothing is used except the main meteor.

+6
source share

It is possible to use the Meteor livedata package (their implementation of DDP) without the need to create a database on the server. This was shown by Avital Oliver , and below I will indicate the corresponding part.

The magic happens here:

 if (Meteor.isServer) { TransientNotes = new Meteor.Collection("transientNotes", {connection: null}); Meteor.publish("transientNotes", function () { return TransientNotes.find(); }); } if (Meteor.isClient) { TransientNotes = new Meteor.Collection("transientNotes"); Meteor.subscribe("transientNotes"); } 

The connection: null setting does not indicate a connection (see Meteor Docs).


Akshat suggested using streams. I cannot reply to his comment due to lack of reputation, so I will put it here. The package to which it refers is no longer supported (see the author’s tweet ). I recommend using the yuukan: streamy package (look at Atmosphere) or use the SockJS core library used in Meteor itself - you can learn how to do this by going through the Meteor code and see how Meteor.server.Stream_server and Meteor are Used. connection._stream, which is what the Streamy package does.

I tested the implementation of the Streamy chat example and found that performance was negligible, but that was only on rudimentary tests. Using the first approach, you get the benefits of minimongo implementation (for example, finding) and Meteor reactivity. Reactivity is possible with Streamy, although it does through things like using ReactiveVar's.

+3
source share

There is a way! At least theoretically, the protocol used by Meteor to synchronize between the client and server is called DDP. Specification here

And although there are examples here and here of people implementing their own DDP clients, I’m afraid they haven’t seen examples of DDP server implementations. But I think the protocol is simple and suggested that it would not be so difficult to implement.

+1
source share

All Articles