vechain / thor

A general purpose blockchain highly compatible with Ethereum's ecosystem
GNU Lesser General Public License v3.0
797 stars 247 forks source link

Feature Request - WS provider for easier web3 event handling #91

Closed decent-dev closed 6 years ago

decent-dev commented 6 years ago

At the moment it seems like we're forced to use getPastEvents() to retrieve event logs from contracts that we intend to subscribe to.

Polling getPastEvents() every X seconds is proving to be an inconvenience especially when designing dapps/scripts that depend on tracking events and acting on changes that have been made.

Also subscriptions can't seem to be leveraged to track general events like say newBlockHeaders - which would have made certain things easier during development of dapps/scripts etc.

If we could use a web socket provider with Thor/Thorify, similar to how we do it with Geth/Web3 now - it'd aid Thor based development immensely.

qianbin commented 6 years ago

Nice suggestion. It's time to make a subscription module.

qianbin commented 6 years ago

start working on branch subscription

samvaughton commented 6 years ago

To add onto this supporting IPC too would be great to keep feature parity with Eth. I personally use IPC to record activity on the blockchain and it is the preferred method if you do not need to expose the node.

I reckon a lot of projects that require their own node without exposing it would prefer to use IPC. If they are porting code over from Ethereum it would make it less painful.

Thoughts?

qianbin commented 6 years ago

@samvaughton IMO, there's no key differences between loopback and unix-socket(named pipe), except the level of security. loopback allows all processes to access, while unix-socket is protected by file system permissions.

samvaughton commented 6 years ago

@qianbin Sorry if I'm getting confused, so this subscription module will be for web sockets? Will I need to use a web socket if I am reading the data from a Vechain node on the same system? Or is there already a IPC type implementation that can be used for observing new block headers etc?

qianbin commented 6 years ago

@samvaughton

so this subscription module will be for web sockets?

Not for websocket, but using websocket to streaming messages, e.g. new blocks, events, transfers.

Will I need to use a web socket if I am reading the data from a Vechain node on the same system? Or is there already a IPC type implementation that can be used for observing new block headers etc?

The subscription module is still under developing. Polling is the only choice now.

qianbin commented 6 years ago

@decent-dev @samvaughton The subscription module is almost done. You can checkout the master branch to build (make dep before build).

OpenAPI doc is available at http://localhost:8669/doc/swagger-ui/#/Subscriptions

Examples to subscribe new blocks:

javascript in web browser

let ws = new WebSocket('ws://localhost:8669/subscriptions/block')
ws.onmessage = console.log

for wscat

# npm i -g wscat 
wscat -c ws://localhost:8669/subscriptions/block

or curl

curl -v --header "Connection: Upgrade" --header "Upgrade: websocket" --header "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" --header "Sec-WebSocket-Version: 13"  http://localhost:8669/subscriptions/block
samvaughton commented 6 years ago

@qianbin Awesome, will check it out soon! Cheers