Skip to content

Commit

Permalink
subscription.js Emit 'connected' event with subscription.id (web3#3028)
Browse files Browse the repository at this point in the history
* Update subscription.js

* Updates Changelog and Docs
  • Loading branch information
lbertenasco authored and nachomazzara committed Jun 4, 2020
1 parent f6e8c9e commit 141a2fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Released with 1.0.0-beta.37 code base.
### Fixed

- ``websocket`` dependency fixed (#2971, #2976)
- ``requestOptions`` added to ``WebsocketProvider`` (#2979)
- ``requestOptions`` added to ``WebsocketProvider`` (#2979)
- Node >= v8.0.0 support (#2938)

## [Unreleased]
Expand All @@ -58,6 +58,7 @@ Released with 1.0.0-beta.37 code base.
- Add `eth.getChainId` method (#3113)
- Minified file added to web3 package (#3131)
- The transaction confirmation workflow can now be configured (#3130)
- Emit `connected` event on subscription creation (#3028)
- TypeScript type definitions added for all modules (#3132)
- Bloom filters added to web3.utils (#3137)

Expand Down
4 changes: 4 additions & 0 deletions docs/web3-eth-contract.rst
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ Returns
- ``"data"`` returns ``Object``: Fires on each incoming event with the event object as argument.
- ``"changed"`` returns ``Object``: Fires on each event which was removed from the blockchain. The event will have the additional property ``"removed: true"``.
- ``"error"`` returns ``Object``: Fires when an error in the subscription occours.
- ``"connected"`` returns ``String``: Fires once after the subscription successfully connected. Returns the subscription id.


The structure of the returned event ``Object`` looks as follows:
Expand All @@ -979,6 +980,9 @@ Example
filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
fromBlock: 0
}, function(error, event){ console.log(event); })
.on("connected", function(subscriptionId){
console.log(subscriptionId);
})
.on('data', function(event){
console.log(event); // same results as the optional callback above
})
Expand Down
9 changes: 9 additions & 0 deletions docs/web3-eth-subscribe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Returns
- ``on("data")`` returns ``Object``: Fires on each incoming log with the log object as argument.
- ``on("changed")`` returns ``Object``: Fires on each log which was removed from the blockchain. The log will have the additional property ``"removed: true"``.
- ``on("error")`` returns ``Object``: Fires when an error in the subscription occurs.
- ``on("connected")`` returns ``String``: Fires once after the subscription successfully connected. Returns the subscription id.

----------------
Notification returns
Expand Down Expand Up @@ -189,6 +190,7 @@ Returns

- ``"data"`` returns ``Object``: Fires on each incoming block header.
- ``"error"`` returns ``Object``: Fires when an error in the subscription occurs.
- ``"connected"`` returns ``Number``: Fires once after the subscription successfully connected. Returns the subscription id.

The structure of a returned block header is as follows:

Expand Down Expand Up @@ -230,6 +232,9 @@ Example
console.error(error);
})
.on("connected", function(subscriptionId){
console.log(subscriptionId);
})
.on("data", function(blockHeader){
console.log(blockHeader);
})
Expand Down Expand Up @@ -340,6 +345,7 @@ Returns
- ``"data"`` returns ``Object``: Fires on each incoming log with the log object as argument.
- ``"changed"`` returns ``Object``: Fires on each log which was removed from the blockchain. The log will have the additional property ``"removed: true"``.
- ``"error"`` returns ``Object``: Fires when an error in the subscription occurs.
- ``"connected"`` returns ``Number``: Fires once after the subscription successfully connected. Returns the subscription id.

For the structure of a returned event ``Object`` see :ref:`web3.eth.getPastEvents return values <eth-getpastlogs-return>`.

Expand All @@ -364,6 +370,9 @@ Example
if (!error)
console.log(result);
})
.on("connected", function(subscriptionId){
console.log(subscriptionId);
})
.on("data", function(log){
console.log(log);
})
Expand Down
1 change: 1 addition & 0 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Subscription.prototype.subscribe = function() {
this.options.requestManager.send(payload, function (err, result) {
if(!err && result) {
_this.id = result;
_this.emit('connected', result);

// call callback on notifications
_this.options.requestManager.addSubscription(_this.id, payload.params[0] , _this.options.type, function(err, result) {
Expand Down

0 comments on commit 141a2fc

Please sign in to comment.