Web3.js: Unsubscribe from listening to events in web3 1.0

Created on 26 Apr 2018  ·  6Comments  ·  Source: ChainSafe/web3.js

Is it possible to unsubscribe from listening to events in 1.0?

I don't see anything in the docs or when checking the object in js either:
https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#events

Most helpful comment

After calling Contract.events.EventName you get a subscription in a response. It has unsubscribe method.

All 6 comments

After calling Contract.events.EventName you get a subscription in a response. It has unsubscribe method.

Does anyone have an example of this? I don't see an unsubscribe method in the Subscription response:

Subscription {id: null, callback: ƒ, arguments: Array(1), _reconnectIntervalId: null, options: {…}, …}
arguments: [{…}]
callback: ƒ (_x, _x2)
id: "0x6d7919d45ee718d3322556d20a4086d6"
options: {subscription: {…}, type: "eth", requestManager: RequestManager, params: {…}}
subscriptionMethod: "logs"
_reconnectIntervalId: null
__proto__: EventEmitter

I'm using web3 version 1.0.0-beta.36

@pors it's under prototype

_reconnectIntervalId: null
​arguments: Array [ {…} ]
​callback: function _on()
​id: "0x0dfc42cec71a4745"
​options: Object { subscription: {…}, type: "eth", requestManager: {…}, … }
​subscriptionMethod: "logs"
​<prototype>: {…}
    _extractCallback: function _extractCallback()
    _formatInput: function _formatInput()
    ​​_formatOutput: function _formatOutput()
    ​​_toPayload: function _toPayload()
    ​​_validateArgs: function _validateArgs()
    ​​constructor: function Subscription()
    ​​subscribe: function subscribe()
    ​​unsubscribe: function unsubscribe()

Does anyone have an example of this? I don't see an unsubscribe method in the Subscription response:

Subscription {id: null, callback: ƒ, arguments: Array(1), _reconnectIntervalId: null, options: {…}, …}
arguments: [{…}]
callback: ƒ (_x, _x2)
id: "0x6d7919d45ee718d3322556d20a4086d6"
options: {subscription: {…}, type: "eth", requestManager: RequestManager, params: {…}}
subscriptionMethod: "logs"
_reconnectIntervalId: null
__proto__: EventEmitter

I'm using web3 version 1.0.0-beta.36

1) the unsubscribe method does exist
2) when the id is still null, unsubscribe doesn't work fully (callbacks are still functioning)

any solution ?

@imbaniac @nivida @elie222 , I wanted to know what is the use of unsubscribe ? Is it required that i should unsubscribe my contract subscription after some time interval?

Was this page helpful?
0 / 5 - 0 ratings