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

How to use this library connect mqtt websocket and send message? #2808

Closed
AyanamiRei0001 opened this issue May 11, 2023 · 8 comments
Closed

Comments

@AyanamiRei0001
Copy link

Hi, I want to use this library send messages to the mosquitto/emqx or other mqtt library websocket port

my computer system is ubuntu 20.04, mosquitto version is 2.0.15, and my another computer is using EMQX dashboard to subscribe the topic message, look at this EMQX dashboard listeners port
image

I am a c++ noob, and I have written a publisher demo using the default port of Mosquito, successfully publishing the topic message to EMQX, and EMQX also received the data

mosquitto_lib_init();
mosq = mosquitto_new(nullptr, true, nullptr);
if (!mosq)
{
    std::cerr << "Failed to create Mosquitto instance" << std::endl;
    return 1;
}
const char *host = "my_another_computer_ip_address";
int port = 1883;
int connect_result = mosquitto_connect(mosq, host, port, 5);
if (connect_result != MOSQ_ERR_SUCCESS) 
{
    std::cerr << "Failed to connect to MQTT broker: " << mosquitto_strerror(connect_result) << std::endl;
    return 1;
}
const char *topic = "testtopic";
std::string message = "Hello, MQTT!";
std::cout << "I Publish Message: " << message << std::endl;
connect_result = mosquitto_publish(mosq, NULL, topic, message.size(), message.c_str(), 0, false);
if (connect_result != MOSQ_ERR_SUCCESS) 
{
   std::cerr << "Failed to publish message: " << mosquitto_strerror(connect_result) << std::endl;
   return 1;
}
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();

EMQX subscribe messages receivced:
image

Next, I want to send message to the default websocket port(8083) of EMQX , but I haven't found a suitable method to implement it. Please help me, thank you very much.

@summer-days
Copy link

I have the same doubt, I see you closed this issue, is it convenient to point it out?

@AyanamiRei0001
Copy link
Author

我也有同样的疑问,我看到你关闭了这个问题,方便指出吗?

你好朋友,我在这个库中也没有找到这种方式,所以我去找了另一个库来实现这个需求,然后关闭了这个问题。你可以尝试“mongoose”,它应该可以满足你的需求

@AyanamiRei0001
Copy link
Author

我也有同样的疑问,我看到你关闭了这个问题,方便指出吗?

你好朋友,我在这个库中也没有找到方法,所以我找到了另一个库来实现这个需求这种,然后关闭了这个问题。你可以尝试“mongoose”,它应该可以满足你的需求

But there is also a problem with mongoose library, which is that after EMQX is greater than version 3.0, publishing content requires adding fields, otherwise sending will fail

@summer-days
Copy link

我也有同样的疑问,我看到你关闭了这个问题,方便指出吗?

你好朋友,我在这个库中也没有找到方法,所以我找到了另一个库来实现这个需求这种,然后关闭了这个问题。你可以尝试“mongoose”,它应该可以满足你的需求

But there is also a problem with mongoose library, which is that after EMQX is greater than version 3.0, publishing content requires adding fields, otherwise sending will fail

Okay, thank you very much.

@Mike0675
Copy link

What was the solution now? I don´t get it?
Thanks

@summer-days
Copy link

summer-days commented Nov 20, 2023

What was the solution now? I don´t get it? Thanks

i did't solve this problem. i try use the websocketpp lib, while publish/subcribe message, packet by mosquitto,send by websocketpp。 But there are a lot of things that need to be changed here, and the specific changes are all in the company's code. If the project is in the selection stage, it is recommended to choose the MQTT lib with websocket function

@AyanamiRei0001
Copy link
Author

What was the solution now? I don´t get it? Thanks

i did't solve this problem. i try use the websocketpp lib, while publish/subcribe message, packet by mosquitto,send by websocketpp。 But there are a lot of things that need to be changed here, and the specific changes are all in the company's code. If the project is in the selection stage, it is recommended to choose the MQTT lib with websocket function

You can see my code repository. it has a test demo to use MQTT lib with websocket function

@AyanamiRei0001
Copy link
Author

What was the solution now? I don´t get it? Thanks

Hi, I am now using other MQTT lib to implement this feature.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants