Sending events from server to client (s) in Meteor

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

I want to send events to clients with some user data. While the meteorite is very good at this with collections, in this case the added complexity and storage are not needed.

There is no need for Mongo repositories or local collections on the server. The client should only be warned that he received an event from the server and acts in accordance with the data.

I know this is pretty easy with sockjs, but it is very difficult to access sockjs from the server.

Meteor.Error does something similar to this.

+8
events meteor
source share
4 answers

Now the package is deprecated and does not work for versions> 0.9

You can use the following package, which was originally designed to send messages from clients-server-clients

http://arunoda.imtqy.com/meteor-streams/

No collection, no mongodb behind, use as it should (not verified):

 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); } 
+4
source share

You must use Collections.

“Added complexity and storage” is not a factor if all you do is create a collection, add one property to it and update it.

Collections are just a form for exchanging data between the server and the client, and they are usually based on mongo, which is very nice if you want to use them as a database. But at their most basic, it's just a way of saying, “I want to store some information known as X,” which intercepts the publish / subscribe architecture you should use.

In the future, other databases will be introduced in addition to Mongo. I could see that at some point there is a smart package that shares collections up to their most basic functions, as you suggest. Perhaps you could write this!

+2
source share

I feel that @Rui and the fact of using the collection just to send a message seem cumbersome.
At the same time, when you have several messages to send, it’s convenient to have a collection with a name, like settings or similar ones, where you save them.


+1
source share

The best package I've found is Streamy. It allows you to send to all or only one specific user.

https://github.com/YuukanOO/streamy

 meteor add yuukan:streamy 

Send a message to everyone:

 Streamy.broadcast('ddpEvent', { data: 'something happened for all' }); 

Listen to the message on the client:

 // Attach an handler for a specific message Streamy.on('ddpEvent', function(d, s) { console.log(d.data); }); 

Send a message to one user (by id)

 var socket = Streamy.socketsForUsers(["nJyQvECmkBSXDZEN2"])._sockets[0] Streamy.emit('ddpEvent', { data: 'something happened for you' }, socket); 
+1
source share

All Articles