MQTT SurgeMq Problems

Im currently trying to read out the message I sent from a client to a mqtt broker on my server. My plan is to use a mqtt client on the server, but Im getting an error.

Code for receiving messages

func receiveMsg() {
mqttClient.Connect("localhost:1883", nil)
msg := message.NewSubscribeMessage()
msg.AddTopic([]byte("/states"), 0)

/*pubMsg := message.NewPublishMessage()

var pubFunc service.OnPublishFunc = func(msg *message.PublishMessage) error {
	return nil
log.Fatal(mqttClient.Subscribe(msg, nil, pubFunc))


panic: runtime error: invalid memory address or nil         pointer dereference
[signal 0xb code=0x1 addr=0x98 pc=0x5bc496]

goroutine 1 [running]:
panic(0x8e47a0, 0xc82000a100)
    /usr/lib/go/src/runtime/panic.go:481 +0x3e6*service).writeMessage(0x0, 0x7fdcf9dbbd98, 0xc820074480, 0x0, 0x0, 0x0)
    /home/ubuntu/go/src/ +0x86*service).subscribe(0x0, 0xc820074480, 0x0, 0xa6ae98, 0x0, 0x0)
    /home/ubuntu/go/src/ +0xd5*Client).Subscribe(0xbff620, 0xc820074480, 0x0, 0xa6ae98, 0x0, 0x0)
    /home/ubuntu/go/src/ +0x4f
    /home/ubuntu/go/mqtt.go:60 +0x243
    /home/ubuntu/go/main.go:83 +0x4f9

How can I resolve this error and is there a better way of doing the same thing?

Can’t tell from the code provided what’s missing (and I’m not familiar with the package), but looking at the package source code it’s obvious that the “service” has not been properly initialized and ‘this’ is ‘nil’.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.