denglerchr / Mosquitto.jl

Eclipse Public License 2.0
15 stars 2 forks source link

07_subscribe_loop_forever.jl does not work locally. #21

Closed eduardkieser closed 6 months ago

eduardkieser commented 7 months ago

When running the 07_subscribe_loop_forever.jl and 08_publish_loop_forever.jl example it works with the test server, but if I replace it with my localhost broker the subscriber does not seem to subscribe properly.

Testing (as on main 2731c2608e2a1c82a3004feebe1ff8feb10abe30) run 07 startup is good:

$ julia 07_subscribe_loop_forever.jl 
Connection of client mFIONEJPe3fleM5 successfull (MOSQ_ERR_SUCCESS), subscribing to eduard/#

run 08 all looks good, see output on 07 terminal:

$ julia 07_subscribe_loop_forever.jl 
Connection of client SOaBUdGHiM2Abhz successfull (MOSQ_ERR_SUCCESS), subscribing to eduard/#
Message 1:
        topic: eduard/julia     message:BBHello World from...
Message 2:
        topic: eduard/julia     message:BBHello World from...
Message 3:
        topic: eduard/julia     message:BBHello World from...
Message 4:
        topic: eduard/julia     message:BBHello World from...

Now if I replace "test.mosquitto.org" with "localhost", the start up of 07 still looks good, but if I run 08 I don't see the messages printed on the 07 terminal. I think the problem is in 07 because if I set up a subscriber using: mosquitto_sub -h localhost -p 1883 -t 'eduard/#' -v I do see the messages come through via the local broker.

Additionally, if I look at the output of my local broker I can see it connect to the 07 client and get the messages from the 08 publisher, but I don't see it trying to pass on the message the the 07 client. The client does seem to be connected because I see the occasional ping-request and response between broker and the 07 client.

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 32 × 13th Gen Intel(R) Core(TM) i9-13900HX
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, goldmont)
Threads: 1 default, 0 interactive, 1 GC (on 32 virtual cores)

it looks a lot like #19 , but might be unrelated..

denglerchr commented 6 months ago

Does it work if you use the loop() calls manually? This loop_forever() is a bit troublesome and I dont seem to find out why, maybe I should remove or hide it :P

eduardkieser commented 6 months ago

Hi, yes it seems to work well enough with the manual loop() call. I would be able to make it work better if I could figure out if there are still things in the upload Q. In my case I'm trying to use it for inter process communication, so I want to shove quit a lot of data through. It seems like the publish() command does not wait for the upload to finish, so if I do a manual loop() in my main loop then the program happily publishes and exits before all the data got to the broker. My work around is to add another fixed length for loop with some loop() and sleep commands to allow all the messages to be uploaded, but I'm sure there must be a more elegant way. Is it possible for the client to figure out if there are still messages to upload?

denglerchr commented 6 months ago

I dont know of a way to check an output queue for Mosquitto, but Mosquitto can give feedback, when the message was transfered to the broker. This is basically done when setting the waitcb flag, as is done in example 08 (internally another callback is done by Mosquitto, putting the message id in a channel). I think however that for qos 0, it just returns immediately as it doesnt wait for feedback from the broker in any way, so I recommend using this with at least qos 1. Unfortunately this makes the publish command block, so loop_forever is required for it currently.

I think I could change the publish command to also return the message ID, then the arrival of the message at the broker could be checked without using the loop_forever

denglerchr commented 6 months ago

I made an update on the main branch. Example 8 shows how to publish and wait for broker feedback without using loop_forever. I also noticed that a problem might have existed in the old example 8 (now example 9) as I was running loop_forever with @async there.

Let me know if this update helps you. You need to checkout main actually.

denglerchr commented 6 months ago

Some update on this. I release the 0.9 which allows you to check if a message was published. I also removed the export from loop_forever and recommend using loop instead, an example is shown in 08_publish_puback.jl. Also, I figured out there is a way to check if there is something to be sent, called want_write . I just didnt implement it properly, but I put this on my list https://github.com/denglerchr/Mosquitto.jl/issues/23 . Closing this one for now, as I believe you can use loop and I cannot reproduce the issues with loop_forever on my side.