Real-time Ethereum streams with EthVigil Websockets
EthVigil exposes a Websocket API to registered developers that can deliver streaming information on transactions, event logs, contract deployments in real time.
You will need to copy over the API write key from the EthVigil dashboard. Connect your Websocket client code to the endpoint wss://beta.ethvigil.com/ws
State transitions
Client connects with API key
1.1. If API key is valid, an acknowledgement is returned by server along with a sessionID
1.2. If not, a non-acknowledgement(NACK) is sent. Go to 7.
Client sends a periodic heartbeat (say every 15 seconds) along with the authenticated sessionID
Server responds to heartbeats for authenticated sessionID
Server pushes payloads to client corresponding to contract deployments, transaction acitivity on contracts and emitted event logs
Client can send a disconnect request with API key
Disconnection is acknowledged by server
Connection is closed
Message formats
Authentication and connection initialization
The client sends a message of the following format. The read API key can be found from the CLI by calling ev-cli accountinfo
or from EthVigil Swagger when you open up any contract in there.
The server responds with an acknowledgement if it is a valid API key.
Else it responds with a non-acknowledgement and disconnects.
Heartbeat messages to keep a connection alive
The client is expected to send periodic heartbeat messages, say, every 15-30 seconds that also specifies the sessionID returned during connection authentication.
The server responds with the same message.
Transaction activity on contracts
Any contracts that you have deployed or retrospectively verified through EthVigil, are monitored and details of transactions sent to it are delivered to the client.
Such messages can be identified by the "type": "contractmon"
field.
Event logs
Transactions can also emit event logs. EthVigil delivers these updates too to authenticated Websocket clients.
Such messages can be identified by the "type": "event"
field.
One-time transaction monitoring
These messages are usually automatically delivered on confirmation of contract deployments. We have not yet opened up the registration command to subscribe to these updates.
Such messages can be identified by the "type": "otm"
field.
Example implementations
Python
You can find an implementation of the client side protocol in Python in our SDK repository on Github..
You can also find this code in action in a working example in a post on our engineering blog.
JavaScript
The following code is meant for client side browser apps to take full advantage of our Websocket API using vanilla JS. It has zero dependencies and can independently run without the need for a backend server. NodeJS users can use a library like ws
to do something similar on the backend as necessary.