PubSub client constructor question

Consider the following code segment:

WiFiClient xyzzy;
PubSubClient client(xyzzy);

My notes say:
// Declare an object of class WiFiClient
// Declare an object of class PubSubClient, which receives as input the constructor previously defined with WiFiClient.
// The constructor MUST be unique on the network.

My question. I have always taken care to make sure the constructor is always different in different programs, but, is this necessary?

No idea what you just said or what constructor has anything to do with all this ( most likely you call a constructor something else which makes it confusing) You will have to explain what you mean

"The constructor MUST be unique on the network" doesn't make sense.
you can always use

WiFiClient wifiClient;
PubSubClient client(wifiClient);

Where does the note come from?

Did you mean you can’t do

WiFiClient xyzzy;
PubSubClient client1(xyzzy);
PubSubClient client2(xyzzy);

(Which is likely a correct assumption)

1 Like

I don't recall, it's just been in my notes for a few years and I always use a different constructor in all sketches.

The docs say: "... Declare an object of class [PubSubClient], which receives as input of the constructor the previously defined WiFiClient."

My question is, does this constructor have to be different from other nodes on the LAN or is its scope just this sketch?

it's just a local instance in your code. There is no unique ID or whatever. So it just needs to be dedicated to ONE PubSubClient instance

ie you (probably) can do this (but why would be the question)

WiFiClient client1;
WiFiClient client2;
PubSubClient psClient1(client1);
PubSubClient psClient2(client2);

but you can't do this

WiFiClient client1;
PubSubClient psClient1(client1);
PubSubClient psClient2(client1); // don't 😱

➜ don't share a WiFiClient with two instances of the PubSubClient class

as input of the constructor. The instance of the class is passed as an argument to the constructor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.