I'm trying a simple code:
package main
import (
"fmt"
zmq "github.com/alecthomas/gozmq"
)
func main() {
context, _ := zmq.NewContext()
defer context.Close()
// Socket to receive messages on
receiver, _ := context.NewSocket(zmq.PULL)
defer receiver.Close()
receiver.Connect("tcp://localhost:5557")
// Process tasks forever
for {
msgbytes, _ := receiver.Recv(0)
fmt.Println("received")
fmt.Println(string(msgbytes))
}
}
In NodeJS I send messages like this:
console.log(payload);
sender.send(JSON.stringify(payload));
I can see the json in the console, so sender.sen()
is actually sending things. Also, the output from the .go
program for each payload is:
received
[]
received
[]
There's no output. I've searched the GoDocs for the Recv
method and there's no separation like recv_json, recv_message, etc
like in other languages, it's all bytes. So what's happening? I'm sending a string because it's sent as stringfy, right?
UPDATE
As Nehal said below, I changed the import statement to the official rep, and this is the new code:
package main
import (
"fmt"
zmq "gopkg.in/zeromq/goczmq.v4"
)
func main() {
// Socket to receive messages on
receiver, _ := zmq.NewPull("tcp://*:5557")
defer receiver.Destroy()
// Process tasks forever
for {
request, _ := receiver.RecvMessage()
fmt.Println("received")
fmt.Println(request)
}
}
But this time 'received' isn't even printed, it seems that no message is being received at all
Server in go:
import (
"fmt"
zmq "gopkg.in/zeromq/goczmq.v4"
)
func main() {
// Socket to receive messages on
receiver, err := zmq.NewPull("tcp://*:5557")
if err != nil {
panic(err)
}
defer receiver.Destroy()
// Process tasks forever
for {
request, err := receiver.RecvMessage()
if err != nil {
panic(err)
}
fmt.Printf("Received: '%s'
", request)
}
}
Client in Node.js:
var zmq = require('zmq')
, sock = zmq.socket('push');
sock.connect('tcp://127.0.0.1:5557');
setInterval(function(){
console.log('Sending data');
sock.send(JSON.stringify({'msg': 'Hi There!'}));
}, 500);
Server Side:
$ go run a.go
Received: '[{"msg":"Hi There!"}]'
Received: '[{"msg":"Hi There!"}]'
...
Client Side:
$ node a.js
Sending data
Sending data
...
RecvMessage Documentation: https://godoc.org/github.com/zeromq/goczmq#Sock.RecvMessage
Node.js package: https://github.com/JustinTulloss/zeromq.node
Excellent zmq examples in go: https://github.com/booksbyus/zguide/tree/master/examples/Go
Nice getting started tutorial: http://taotetek.github.io/oldschool.systems/post/goczmq1/