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()
pubMsg.SetTopic([]byte("/states"))*/

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

Error:

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
github.com/influxdata/surgemq/service.(*service).writeMessage(0x0, 0x7fdcf9dbbd98, 0xc820074480, 0x0, 0x0, 0x0)
    /home/ubuntu/go/src/github.com/influxdata/surgemq/service/sendrecv.go:268 +0x86
github.com/influxdata/surgemq/service.(*service).subscribe(0x0, 0xc820074480, 0x0, 0xa6ae98, 0x0, 0x0)
    /home/ubuntu/go/src/github.com/influxdata/surgemq/service/service.go:288 +0xd5
github.com/influxdata/surgemq/service.(*Client).Subscribe(0xbff620, 0xc820074480, 0x0, 0xa6ae98, 0x0, 0x0)
    /home/ubuntu/go/src/github.com/influxdata/surgemq/service/client.go:158 +0x4f
main.receiveMsg()
    /home/ubuntu/go/mqtt.go:60 +0x243
main.main()
    /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.