Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending events from server to client(s) in Meteor

Tags:

events

meteor

Is there a way to send events from the server to all or some clients without using collections.

I want to send events with some custom data to clients. While meteor is very good in doing this with collections, in this case the added complexity and storage its not needed.

On the server there is no need for Mongo storage or local collections. The client only needs to be alerted that it received an event from the server and act accordingly to the data.

I know this is fairly easy with sockjs but its very difficult to access sockjs from the server.

Meteor.Error does something similar to this.

like image 231
Rui Gonçalves Avatar asked Oct 26 '12 13:10

Rui Gonçalves


3 Answers

The package is now deprecated and do not work for versions >0.9

You can use the following package which is originally aim to broadcast messages from clients-server-clients

http://arunoda.github.io/meteor-streams/

No collection, no mongodb behind, usage is as follow (not tested):

stream = new Meteor.Stream('streamName'); // defined on client and server side

if(Meteor.isClient) {
    stream.on("channelName", function(message) {
      console.log("message:"+message);
    });
}

if(Meteor.isServer) {
    setInterval(function() {
      stream.emit("channelName", 'This is my message!');
    }, 1000);
}
like image 105
Flavien Volken Avatar answered Oct 14 '22 00:10

Flavien Volken


You should use Collections.

The "added complexity and storage" isn't a factor if all you do is create a collection, add a single property to it and update that.

Collections are just a shape for data communication between server and client, and they happen to build on mongo, which is really nice if you want to use them like a database. But at their most basic, they're just a way of saying "I want to store some information known as X", which hooks into the publish/subscribe architecture that you should want to take advantage of.

In the future, other databases will be exposed in addition to Mongo. I could see there being a smart package at some stage that strips Collections down to their most basic functionality like you're proposing. Maybe you could write it!

like image 2
Rahul Avatar answered Oct 13 '22 22:10

Rahul


I feel for @Rui and the fact of using a Collection just to send a message feel cumbersome.
At the same time, once you have several of such message to send around is convenient to have a Collection named something like settings or similar where you keep these.


like image 1
dfucci Avatar answered Oct 14 '22 00:10

dfucci