[Ethereum] How to unsubscribe logs / events

eventslogssolidity

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); });

Best Answer

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