Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscription.js Emit 'connected' event with subscription.id #3028

Merged
merged 7 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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