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

Automatic connect function #41

Open
skyformat99 opened this issue Oct 24, 2017 · 9 comments
Open

Automatic connect function #41

skyformat99 opened this issue Oct 24, 2017 · 9 comments
Assignees

Comments

@skyformat99
Copy link

Can you add automatic connect function? thanks.
For instance, When the broker restarts, ping/pong test cannot auto connect to the broker server.

like as:
Recently, I learned about paho. MQTT. C, and found the function as:
void connectionLost(void* context, char* cause)
url:
https://github.com/eclipse/paho.mqtt.c/blob/master/src/samples/paho_c_pub.c

so, Maybe the following function names are more appropriate:
typedef void (*lmqtt_client_on_connect_lost)(void *, lmqtt_connect_t *, int);

disconnect: Disconnect actively by itself
connect_lost: Passive disconnect.

@dgarske
Copy link
Contributor

dgarske commented Oct 24, 2017

Hi skyformat99,

Thanks for the excellent suggestion! The "Add automatic re-connect" is already on our feature request list, but I will give it a bump up on the list. I expect this feature to be added by the end of this 2017. How urgent is the automatic reconnect requirement for using wolfMQTT?

Thanks,
David Garske, wolfSSL

@skyformat99
Copy link
Author

@dgarske
Thank you for your reply!

This feature should be a very useful feature. For example, in 4G network communication, there are often network failures, and if there are automatic reconnect functions (including automatic pub and sub), it will be much more convenient.

@skyformat99
Copy link
Author

Have any progress about it?

@skyformat99 skyformat99 reopened this Nov 24, 2017
@dgarske dgarske self-assigned this Apr 19, 2018
@BeIllegalBeagle
Copy link

BeIllegalBeagle commented May 11, 2018

I'm in need of the same feature, I tried to see if I could loop the rc = client->net->write(client->net->context, buf, buf_len, timeout_ms);
function in mqtt_socket.c until a successful connection to the broker occurs but the program simply stalls when a broker is not detected, making the 'rc' variable a bit useless(unless I'm doing it wrong)

@dgarske
Copy link
Contributor

dgarske commented May 11, 2018

This feature is being added and should be available next week. I will post an update here when its ready. Thanks, David Garske

@dgarske
Copy link
Contributor

dgarske commented May 16, 2018

Hi All,

I've pushed a basic disconnect callback to a pull request here:
#69

The callbacks are issued from the mqtt_packet layer and the original error code is then sent up the stack. The idea is for you to set this disconnect callback in your application and then have it trigger some some type of reconnect action. I don't think its a good idea to do the reconnect in the callback, but instead set a state or context variable that triggers this in your main thread.

I would like to see an automatic reconnect feature as the next step for this. Let me know your thoughts.

Thanks,
David Garske, wolfSSL

@BeIllegalBeagle
Copy link

Hi @dgarske I'm trying to do the following

  1. Have a reconnection if the broker is not live
  2. Have a reconnection if connection is lost

How would I use this callback you added currently? Within the netwrite thread my program stalls at the 'connect' or 'write' function.

This is the function - static int NetWrite(void *context, const byte* buf, int buf_len, int timeout_ms) inside of mqttnet.c

and this is the line
rc = (int)SOCK_SEND(sock->fd, buf, buf_len, 0);
I don't even get any value of the rc if there is an error here, it just stops here if the broker is not running so I can't do anything further up like you mentioned. What would you suggest?

I will also try to see if I get a better result when I try closing the broker once a connection has been established to the client.

@milamber-ls
Copy link

any update on the automatic reconnect? is there a way to implement it ourselves?

@dgarske
Copy link
Contributor

dgarske commented Aug 5, 2019

Hi All,

The actual socket handling for connect is handled via callbacks, which the consumer of the library is responsible for setting up. If you are waiting for a message MqttClient_WaitMessage and MQTT_CODE_ERROR_TIMEOUT then you might consider doing a "keep-alive" much like our example here: https://github.com/wolfSSL/wolfMQTT/blob/master/examples/mqttclient/mqttclient.c#L508

If you get a response code that is not MQTT_CODE_SUCCESS or MQTT_CODE_CONTINUE (for non-blocking) then you would have received a socket error and you would need to reconnect the socket and call MqttClient_Connect again.

We added a disconnect callback that you can register via MqttClient_SetDisconnectCallback, which will tell you when a network error occurred. This allows you to set a flag indicating your client needs to reconnect. The actual socket reconnect cannot occur in this callback.

Let me know if you have any questions or issues.

Thanks,
David Garske, wolfSSL

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

No branches or pull requests

5 participants