Communicating Back to client from agent

So, before I could use agents, I had a server that runs on my home computer (don’t want that in the long term). The imp would push data to that computer (it has a public IP).

My iPhone app (when running), connects to this server, and keeps the HTTP connection open. When changes come in from the imp, it posts it on the open http connection, which the app gets.

I am moving everything over to imp+agent. The only way I can do this is to poll. The problem is, updates could come at different intervals (polling every 1 second is probably too much for most cases, but might be necessary for others).

So, how can an imp (through the agent) better communicate with my app (which is on a dynamically changing network)?

You can have your iPhone pass it’s network settings to the Imp every times it changes

That isn’t going to reliably work in a NAT’d world. At least my home server is always home and I and my router can port forward. My phone is out and about changing networks, that isn’t going to work.

Polling is the only way I’ve found to do it. I can’t see a way of doing long polling In the future my hope is web sockets will be supported this way the agent could push to the mobile web app.

You can do long polling right now - you do not need to answer an incoming http request to your agent immediately, but instead send the reply when a change comes along.

Right now you can only do this once per connection (so once the phone has got the latest data from the agent it needs to open a new connection for the next update) but it works very well.

Can we see some code? The answer can be hours later?

I believe it’s limited to minutes later, but without traffic most connections (especially from phones, which are very heavily NATed) will have timed out way before hours pass.

I don’t have any example code… but the basic concept is that you do not need to do a response.send() in your http.onrequest handler. You can just save the response object somewhere and call it later (eg when you have new data to tell the phone about).