bitcoin core – bitcoind: How to get rawblock data with ZeroMQ


I am trying to use the zeromq api for bitcoind to get rawblock but its not working with the code I’ve provided below. I am able to get hashtx and rawtx if I remove the filter but not hashblock and rawblock. Not sure if this is an important detail or not but my bitcoind is still syncing so I’m not sure if those topics are only triggered when bitcoind has synced up.

// Implementation of ZeroMQ in node.js.
// From the maintainers of the ZeroMQ protocol.
var zmq = require("zeromq");

// Create a subscriber socket.
const sock = new zmq.Subscriber();
var addr = "tcp://127.0.0.1:28332";

// Initiate connection to TCP socket.
sock.connect(addr);

// Subscribe to receive messages for a specific topic.
// This can be "rawblock", "hashblock", "rawtx", or "hashtx".
sock.subscribe("rawblock");

(async () => {
  for await (const (topic, msg) of sock) {
    console.log(topic.toString())
  }
})();

My bitcoin.conf file looks like:

server=1
txindex=1

# (zeromq)
zmqpubhashblock=tcp://127.0.0.1:28332
zmqpubhashtx=tcp://127.0.0.1:28332
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28332