I am creating a new smartapp and need to receive updates from pubnub by subscribing to it.
I was wondering if anyone has any idea about http long polling and support for pubnub? I searched the forums and documentation with not much luck. I would appreciate if anyone can give me some pointers.
Thanks,
Ranga
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
2
Probably won’t help much, but …
To the best of my knowledge, a SmartApp cannot hold open an http connection longer than 20 seconds.
There is an async http API, for which you can specify the callback method to use when updates are being sent from pubnub. You can also generate a auth_token to receive arbitrary inbound calls via REST-API endpoints.
Also worth noting that the async request, although no one knows what timeout limit it has - most likely the same 20s - as well as the sync http will return data at the end of the connection, not as it arrives, so it is not appropriate for event listening.
If the provider you are listening to does not provide REST event pushing, then the alternative is using an external device such as a Raspberry PI or even an existing full size server to do the listening and send requests to the app when such events happen.
The provider updates via pubnub.com and it uses http long polling which lasts for 300s. As far as I know, pubnub doesn’t support REST event pushing. The async will timeout after 40s but will not return any data until the end of the connection. So, the only way seems to poll it with non async request every few minutes to determine if any changes are present, though not ideal.
The whole idea behind creating the smartapp is to get rid of any delays due to another server. And to keep the setup simple. Adding an external device would not be the best solution in my opinion.
tgauchat
(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy)
6
You may be misunderstanding some critical points about the SmartThings architecture:
It is an Event Driven system (not polling).
Events from physical Things (or services acting as “Things”) are supposed to be handled by Device instances (Device Type Handlers). That’s why DTH’s have the ability to generate an “authentication token” so that they can give it to the remote service for callbacks.
Of course, SmartThings constantly “listens” for Events, but there are limitations on this. It constantly listens for events from Z-Wave and ZigBee devices on the LAN, and it listens for explicit REST-API calls.
SmartApps are intended to run for as short a period as possible. “Web Service” SmartApps are like Device Type Handlers in that they can define REST-API endpoints for callbacks.
But, unfortunately, SmartThings does not expose any long polling. That is non-trivial to implement in their cloud infrastructure, I presume, without either significant performance impact and/or architecture redesigns…
External service is - unfortunatelly - your only way. I believe there are some nodejs libraries for pubnub which can easily be modified to send events to your SmartApp - while there is an extra middle-man server in this equation, it sure beats having to poll every X minutes, you will have real time* events.
*Real time is a very relative term - it is generally considered the time window during which the information is still useful and current. Real time can mean microseconds for an airbag, or minutes for a window shade. The delay an external server you own would introduce is negligible (measured in milliseconds)