1

I'm having trouble receiving WebSocket messages for address-related events. Here is the code that I'm using (trackAddress gets called externally and I'm sure it sends the right thing). I also tried from an online WebSocket client (as seen in the photo) and the response to ping_tx is not the right transaction (the docs state that if you are subscribed to an address it would be that address's last transaction), instead, it returns the most recent on the whole network. It's really frustrating but I do not understand what it is that I am doing wrong. It's as if I'm never really subscribing to it...

You can see the transaction here: https://www.blockchain.com/explorer/addresses/btc/1P8wXFz6F7UdqnfsByZUf6tBxukcp9bJE9

import { WebSocket } from "ws"

const ws = new WebSocket("wss://ws.blockchain.info/inv")

setInterval(() => {
    ws.ping()
}, 30000)

ws.on("open", async() => {
    console.log(`Listener connected to ${ws.url}`)
})

ws.on("close", () => {
    console.log("Listener disconnected")
})

ws.on("message", (data => {
    const res = JSON.parse(data.toString())

    console.log(res)
}))

export const trackAddress = (address: string) => {
    console.log(`Tracking address ${address}`)
    ws.send(JSON.stringify({
        op: "addr_sub",
        addr: address
    }))
}

enter image description here

1 Answer 1

1

I faced the same problem and the solution is to listen to all transactions and to detect the transaction sent to our address in this way, I tried a method and it works fine.

ws.addEventListener('open', () => {
    ws.send(JSON.stringify({
        "op": "unconfirmed_sub"
    }));
});

ws.addEventListener('message', (res) => {
    let data = JSON.parse(res.data);
    let outs = data.x.out;
    let out = outs.find(o => {
        return String(o.addr).toLowerCase() == receiver.toLowerCase();
    });
    if (out) {
        setTimeout(() => {
           startCallback(data);
        }, 6000);
    }
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.