zevv / nmqtt

Native Nim MQTT client library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Subscribe to topic" example does not compile.

Mihara opened this issue · comments

Attempting to compile the example given in the readme:

import nmqtt, asyncdispatch

let ctx = newMqttCtx("nmqttClient")
ctx.set_host("test.mosquitto.org", 1883)
#ctx.set_auth("username", "password")
#ctx.set_ping_interval(30)
#ctx.set_ssl_certificates("cert.crt", "private.key")

proc mqttSub() {.async.} =
  await ctx.start()
  proc on_data(topic: string, message: string) =
    echo "got ", topic, ": ", message

  await ctx.subscribe("nmqtt", 2, on_data)

asyncCheck mqttSub
runForever()

as is with a fresh nimble project (on Nim 1.2.4 and with nmqtt 1.0.3) gets me this:

        ... /home/mihara/Projects/testify/src/testify.nim(16, 12) Error: type mismatch: got <proc (): Future[system.void]{.locks: <unknown>.}>
        ... but expected one of: 
        ... proc asyncCheck[T](future: Future[T])
        ...   first type mismatch at position: 1
        ...   required type for future: Future[asyncCheck.T]
        ...   but expression 'mqttSub' is of type: proc (): Future[system.void]{.locks: <unknown>.}
        ... expression: asyncCheck mqttSub

Now, I will be the first person to admit I don't know heads or tails of how asynchronous procedures work in nim, being new to the language, but this error message doesn't seem very helpful. After much mucking around, I've found that the error message means that the compiler has decided that the mqttSub proc is for whatever reason not GC-safe. That, however, is where I'm stuck from there on: I don't know how to make it GC-safe, and whatever is making it not-GC-safe appears to be caused by a macro. Which I still haven't figured out how to debug.

I can of course write discard mqttSub() instead, and it compiles (and even works, if I put my MQTT server in), but the Nim manual warns against doing that specifically. I'm probably missing something obvious, but in any case, I think the example should be corrected.

Hi @Mihara

Great catch! I have updated the README, so the example works. The problem was missing parentheses:

asyncCheck mqttSub
# to:
asyncCheck mqttSub()

Please let me know, if that works for you.

Yes, it does work. I really was missing something very obvious.

Thanks. :)