Introducing the WebSocket Event Stream API

vrde
The BigchainDB Blog
3 min readApr 19, 2017

--

With BigchainDB version 0.10 [GitHub] we’ve released the first implementation of the WebSocket Event Stream API.

The Event Stream API enables new ways to interact with BigchainDB, making it possible for your application to subscribe to all newly–confirmed transactions that are happening in the system.

Here’s to events! [image by Alberto Granzotto]

From now on, if you are running BigchainDB version 0.10, you can open web socket to host:9985 and listen to the transactions that has been just confirmed. As soon as a new transaction changes its status to valid, your program will receive a message from the BigchainDB web socket server with the following payload:

{
"tx_id": "<sha3-256 hash>",
"asset_id": "<sha3-256 hash>",
"block_id": "<sha3-256 hash>"
}

You can use this API to get a “green light” every time you push a new transaction to BigchainDB, meaning that your transaction has been accepted and validated. Or you can use it to track new transfers on a specific asset_id, filtering out all transactions that you don't need. Moreover, we want to highlight that this is a first step towards a more generic, richer events API, keep reading to know what are the next steps!

Under the Hood

WebSocket Protocol

To implement this API we decided to leverage the WebSocket protocol, a widely used web standard that enables real–time communication between two parties. The reasons behind this decision are:

  • WebSockets provide a rich API to events and connections;
  • Every programming language has one or more implementations of WebSockets;
  • Web browsers support WebSockets natively, so “browser only” JavaScript applications will be able to connect to BigchainDB directly.

Aside: Most of our work, and most of our decision making process, is public. If you want to know more about the history of the design of this API, take a look at its first steps.

High Performance Python with aiohttp

WebSocket handling is a good example where an asynchronous implementation can really stand out, compared to a threaded one. We decided to embrace asyncio, the new (as in non provisional) API for asynchronous IO, and specifically aiohttp to handle the HTTP protocol. aiohttp is a well documented, supported, and fast Python module with some amazing benchmark results.

Since the supported use cases are still simple, the implementation of the web socket server is quite short.

Aside: A note for all the pythonistas out there: since BigchainDB supports Python 3.4, we cannot use the new keywords async and await, so we need to stick with the old syntax @asyncio.coroutine and yield from.

What’s Next

The WebSocket Event Stream API is our first step to support more complex use cases. In the future, we may provide streams for other information, such as new blocks, new votes, or invalid transactions. We may also provide the ability to filter the stream for specific qualities, such as a specific output’s public_key.

To enrich even more our support for real time events, we are also starting thinking about a Web Hook API. If you want to help us design it, feel free to comment on the related issue!

Conclusion

Are you happy? Are you sad? What would you like to see next for the WebSocket Event Stream API?

We kept this feature simple because we wanted to have it out sooner rather than later, to understand how it’s perceived by developers, and to improve it accordingly.

Be sure to follow this blog to stay up to date on future releases and to get a first-hand look at additions to the system, what has changed and why. Finally, if you’re a developer using BigchainDB, we want to hear from you. Send us an email at contact@bigchaindb.com and tell us your story.

--

--