<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Zephyr MQTT poll()</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/80746/zephyr-mqtt-poll</link><description>In the Simple Mqtt example I noticed that the whole thing gets blocked by: 
 err = poll(&amp;amp;fds, 1, mqtt_keepalive_time_left(&amp;amp;client)); 
 The keep alive is 60 seconds, this means that I cant to anything unless the keep alive is timed out or I receive an</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 20 Oct 2021 18:40:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/80746/zephyr-mqtt-poll" /><item><title>RE: Zephyr MQTT poll()</title><link>https://devzone.nordicsemi.com/thread/335151?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 18:40:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ace00b47-0eeb-4dc2-bcc9-f8cf40a234b9</guid><dc:creator>FZe</dc:creator><description>&lt;p&gt;Thank you for a the good explanation Didrik&lt;/p&gt;
&lt;p&gt;I will move it to a thread later on and check out the workerque as well.&lt;/p&gt;
&lt;p&gt;I did not know that the OS handles the sleep modes of the SIP, where can I read more on this?&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr MQTT poll()</title><link>https://devzone.nordicsemi.com/thread/335028?ContentTypeID=1</link><pubDate>Wed, 20 Oct 2021 09:19:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b147027-03fc-49d6-b53c-ee83ce4eeb66</guid><dc:creator>Didrik Rokhaug</dc:creator><description>[quote user="FZe"]What I want to understand is how the sockets/poll works.[/quote]
&lt;p&gt;poll() takes a list of sockets, and a timeout. It will then block the thread until either something happens on one of the sockets or it times out.&lt;/p&gt;
&lt;p&gt;The way it is used in the mqtt_simple sample is to take the MQTT socket and the MQTT keepalive period as a timeout. This means that there are two possible reasons for why poll() returns: The keepalive period is up and we need to send a keepalive message, or something happened on the socket (we have received some data or the connection is closed). The code that follows the call to poll() checks which it is, and takes the appropiate action.&lt;/p&gt;
&lt;p&gt;If you shorten the timeout, the worst thing that will happen is that you wake up unnecessary, costing power. If there is any unread data on the socket when you call poll(), poll() will return immediatly, so there is no risk of missing data. You also don&amp;#39;t have to call poll() to keep the connection alive (on a TCP level), but you must call mqtt_live() regularly to keep the MQTT connection alive if there are no other data being sent. Using the timeout in poll() is one way to ensure that you don&amp;#39;t call mqtt_live() more often than you have to.&lt;/p&gt;
[quote user="FZe"]I have my own interrupt that sets a flag, the flag is checked in the main loop, if flag is set then it publishes a message.[/quote]
&lt;p&gt;If I may come with a suggestion here: Rather than polling a flag in the main loop, which is normally blocked by poll() unless you move the MQTT handling to a different thread or shorten the timeout, you can use a &lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/kernel/threads/workqueue.html"&gt;workqueue&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By using a workqueue, you don&amp;#39;t have to poll a flag yourself, instead the work item will be executed the next time the workqueue runs (assuming it is the first item in the queue).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr MQTT poll()</title><link>https://devzone.nordicsemi.com/thread/334687?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 18:26:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2c020c83-a959-472f-bbc8-5f5ac834b377</guid><dc:creator>FZe</dc:creator><description>&lt;p&gt;Ok, I don&amp;#39;t use the DK. I have my own interrupt that sets a flag, the flag is checked in the main loop, if flag is set then it publishes a message.&lt;/p&gt;
&lt;p&gt;What I want to understand is how the sockets/poll works. For instance if I have a short timeout and do other stuff in between does the connection drop? or does messages get lost? I suppose this could happen even if I run different threads as well?&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Zephyr MQTT poll()</title><link>https://devzone.nordicsemi.com/thread/334569?ContentTypeID=1</link><pubDate>Mon, 18 Oct 2021 11:20:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e5b96cc1-3d76-42c5-be17-53d7b2d37bf9</guid><dc:creator>Didrik Rokhaug</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;What exactly is it that you want to do?&lt;/p&gt;
&lt;p&gt;You are correct that poll() will block the thread until something happens on the socket(s) or it times out. But you can run other tasks in other threads if you want to do something else while waiting for MQTT activity.&lt;/p&gt;
[quote user=""]do I need to call some abort_poll function if I want for e.g. publish a message before the timeout?[/quote]
&lt;p&gt;If you want to send something, you can simply send it. E.g. in the mqtt_simple sample, the main thread (the thread that runs the main() function) will check for incoming MQTT messages and keep the connection alive (in the poll() loop).&lt;/p&gt;
&lt;p&gt;At the same time, the dk_buttons_and_leds library checks for button presses in the system workqueue. When a button press is detected, a message is sent to the broker (from the button_handler() function).&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Didrik&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>