Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Ports Mosquittopp #2984

Open
jacob-02 opened this issue Jan 24, 2024 · 1 comment
Open

Multiple Ports Mosquittopp #2984

jacob-02 opened this issue Jan 24, 2024 · 1 comment

Comments

@jacob-02
Copy link

Hello

I am currently using mosquittopp for CPP to facilitate MQTT message communication. Till now I've been using the default port for TCP/IP communication, 1883. There are 3 systems with one master and 2 slave systems and I would like the master system to communicate using 1883 port to one slave and using 9001 to the other slave.

Could someone help me understand if this is possible using mosquittopp? And if so could you please direct me to some examples on the same to help me implement it?

Thank you in advance

@saiganesh377
Copy link

Yes, it is possible to achieve communication using different ports for different connections with Mosquittopp.

To establish MQTT connections with different ports for different systems, you can create separate instances of mosquittopp::lib objects for each connection, and specify the desired port when connecting to the MQTT broker.

Create Separate mosquittopp::lib Instances: Create separate instances of mosquittopp::lib for each MQTT connection you want to establish.

Connect to MQTT Broker with Specified Ports: When connecting to the MQTT broker using mosquittopp::lib::connect(), specify the desired port for each connection. For example, use port 1883 for one connection and port 9001 for another connection.

Handle Message Communication: Implement message publishing and subscription logic within each instance of mosquittopp::lib as needed for your application.

Here's a basic example
#include <mosquittopp.h>
#include

class MyMosq : public mosqpp::mosquittopp
{
public:
MyMosq(const char *id, const char *host, int port) : mosquittopp(id)
{
int rc = connect(host, port, 60);
if (rc != MOSQ_ERR_SUCCESS)
{
std::cout << "Error connecting to MQTT broker: " << mosqpp::strerror(rc) << std::endl;
}
}

void on_connect(int rc) override
{
    if (rc == 0)
    {
        std::cout << "Connected to MQTT broker" << std::endl;
    }
    else
    {
        std::cout << "Failed to connect to MQTT broker: " << mosqpp::strerror(rc) << std::endl;
    }
}

};

int main()
{
// Create instance for master system communicating on port 1883
MyMosq master("master", "localhost", 1883);

// Create instance for slave system communicating on port 9001
MyMosq slave("slave", "localhost", 9001);

// Enter Mosquittopp loop
int rc1 = master.loop_forever();
int rc2 = slave.loop_forever();

return (rc1 == MOSQ_ERR_SUCCESS && rc2 == MOSQ_ERR_SUCCESS) ? 0 : 1;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants