How to communicate between client and server?


#1

I can’t figure out how to do the pattern of sending and receiving messages or pushing and listening events between server and client.

with pub/sub, client can know the newest data, but how to make them know that the data has changed?

thanks


#2

Here’s an official tutorial, it will help you understand Meteor’s basic concepts.


#3

Thanks. I am not new to meteor, and I have done these tutorial several times. Maybe I have got something wrong or missed something.

With pub/sub, we can easily show the current, latest data, but I don’t know how to send message to trigger event, for example, when a new message comes, play some audio once.

With methods, client can poll and compare statues of some data, but it’s better if the server can push something to the client directly when something happen.


#4

If the “message” is based on a collection, then it’s quite simple to get the a pub/sub to make the client respond reactively.

The alternative is a “roll-your-own” publication, which does not require a server-side collection at all. Here’s one I prepared earlier: Best Way to handle a 3rd Party endpoint


#5

Use cursor.observeChanges and play the sound in the added hook.


#6

Awsome solution! Thanks @Steve . By reading the doc, I have one more question:

Before observeChanges returns, added (or addedBefore) will be called zero or more times to deliver the initial results of the query.

does this mean that I will also receive the added events of the initial (first batch) data? how to prevent this and only receive the added events of the following data?


#7

A simple flag will do the trick. There is an example here (last code sample).


#8

@Steve Thank you very much! In my view, it’s a bit tricky due to the low level api, but it does work for my case.