2

There are two ways to subscribe/watch to logs/events:

Subscribe:

var subscription = web3.eth.subscribe('logs', {
    address: '0x123456..',
    topics: ['0x12345...']
}, function(error, result){
    if (!error)
        console.log(log);
});

Unsubscribe:

subscription.unsubscribe(function(error, success){
    if(success)
        console.log('Successfully unsubscribed!');
});

But how do I unsubscribe if I watch the logs / events like this:

myContractWebsocket.events.MyLogName({
    filter: {'orderId': '0x123456...'}, 
    fromBlock: 0
    })
    .on('data', data => { console.log(data); })
    .on('error', error => { console.log(error); });
0

myContractWebsocket.events.MyLogName returns a Subscription response, which has an unsubscribe method. See here for a bit more detail.

  • Unfortunately it doesn't work like that. I get TypeError: subscription.unsubscribe is not a function – Rob Magier Feb 17 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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