Difference between particle.publish() and particle.variable()

Hello everyone !!

Could anyone explain me the entire communication process that takes place during two different cloud functions i.e particle.publish() and particle.variable() respectively?

PS: I searched a lot but could not get any satisfactory answer to this.

Thank You
Soovam

Particle.publish() pushes an event to the cloud (googlable term Server Sent Event) whenever called (probably multiple times during a session).

Particle.variable() only exposes a global C variable to the cloud to be requested - no active communication from the device. Hence Particle.variable() is ever only called once in your code for any particular variable.

However, I’m pretty sure that’s also expained in threads like these
What exactly are webhooks, Particle.publish, Particle.variable, and Particle.function used for?
Particle.variable vs. Particle.publish for Asset Tracker GPS data
Should I use Particle.variable() or Particle.publish()?
and loads more

2 Likes

Thank You for your answer.
But, what exactly does “exposing a variable to the cloud” mean?

It means that the cloud gets informed about a variable in your code that can be requested by means of the name (first parameter) you provide to register it under.

Thanks @ScruffR for the explanation.
Please consider this example.
Suppose I am exposing a variable, say temp., to the cloud. Now, if I want to access the data in ‘temp.’ in CLI, is the communication path as follows?
=> The server sends a request to the cloud to get the most recent temp. data and the cloud requests the device to send the temp. data. Finally, the cloud sends “the data that it gets in response to the request made to the device” back to the server.
Is this the entire communication process that happens meanwhile?

In simple terms, yes (assuming “the server” refers to your machine that triggers the request).

@ScruffR yes, I meant the machine.
Now being said that, could you please explain how would the cloud initiate a communication with the device? I mean, what protocols are used? I found out that almost all the communication protocols initiate communication from the iot device itself.
But however, in the above example, the cloud requests the device to send temp. data.

The device is connected to the cloud using an encrypted link, if you want to get into the details of how that works, it is all in the docs and indeed in here. However that link is essentially invisible to you and you don’t need to concern yourself with how it works as the user interfaces with the cloud not the device.

3 Likes

REST via CoAP.

3 Likes

The exact method of communication varies depending on the device.

Photon and P1s (Wi-Fi) devices use CoAP, AES encrypted, over TCP. The connection is made outbound from the device to the Particle cloud and is kept open. This allows requests to be sent to the Photon even if it’s behind a firewall. It is also safer because no ports are open on the Photon so there’s less attack surface.

Electrons (cellular) use CoAP over DTLS, over UDP. Even though it’s UDP and the cellular network uses NAT, it’s still possible to send requests to the device because there’s a temporary UDP back-channel set up when the Electron sends a packet. This allows requests to be made to the device as well as from the device.

In both case, the connection is mutually authenticated using RSA public/private key pairs.

The CoAP protocol allows a single TCP or UDP connection to handle things like publish, subscribe, variables, functions, and over-the-air code flashing over a single connection.

5 Likes

Thankyou @rickkas7 for your answer.
But here’s another question.
Suppose, I am flashing my photon from the web IDE. Now, as soon as I click the flash button, the code to be flashed first goes to the cloud and then to the photon. Now, my question is : In the network layer, how does the cloud find my photon’s IP in order to interact with the photon (as the photon’s IP is dynamic and the connection has not been initiated by the photon)?

PS: I know this is very basic, but I seriously am a bit confused with this concept. Please help !!

You can't flash code to the Photon unless it is already connected to the cloud, and that connection was initiated by the device at some time prior to you clicking the flash button.

Hello @Ric, but does the connection always stay open? Like, even if the ip address of the photon changes dynamically, if the connection is intact does it always update the cloud with the modified ip of the photon? Because, for interacting with the photon, the cloud needs to know the dynamic IP of the photon for sure.

I assume so. I've never had any problems with my devices not connecting or staying connected (except when I had code errors), but I don't know for sure whether my ip address has changed or not while a device was connected (or even if it can change while it's connected).

As @Ric already explained, since the device establishes the connection to the cloud, the cloud can use that channel to send data back to it and wouldn’t even need to know the local IP of your device.

However, when you want to know the nitty gritty bits of the protocol, you may want to read-up on CoAP which will cover the basic principles to build on.

1 Like