How can I subscribe to many NEST devices from many

2019-10-18 05:14发布

问题:

I am trying to scope a project for a client that wants to be able to subscribe to many (perhaps 10's of thousands) of NEST devices for many different users from one or more Node.js servers. The idea being to aggregate change events from many devices owned by many clients. Could Node.js handle that many simultaneous Firebase change subscriptions? How would that best be managed?

Thanks

回答1:

Firebase engineer here,

The problem with this current approach is that Nest currently only allows a single token to authenticate at a time, and since Firebase clients cache authentication state once authenticated, in order to do this in a single process on one of said servers, you would have to auth, perform the operation, then unauth, and repeat for every operation which isn't very efficient. Listens are particularly inefficient, since this means you need to open up n socket connections, and having to switch between them in this manner somewhat defeats the purpose of using Firebase in the first place (being able to hold the connection open and get pushed updates).

You can solve this in a few ways:

  • Switch to multi-process and multi-server where each process has an open socket and its own auth token. Using our Node or Java client is typically the best option here.
  • Use the streaming REST API to open up multiple connections on the same server.

Hope this helps!



回答2:

You can use Firebase clients in your node.js server, instead of REST Streaming, by using an (undocumented) "context" parameter in the Firebase ref creation call, as follows:

var context = new Firebase.Context();
var ref = new Firebase('wss://developer-api.nest.com', context);
ref.authWithCustomToken(...)