Today I'm releasing Bitsocket, a powerful bitcoin push notification infrastructure for building realtime event driven bitcoin applications.
While Bitcoin has been around for almost a decade, there has never been an easy AND flexible way for developers to programmatically listen to, filter, process, and act upon various patterns of Bitcoin transactions in realtime.
At best you could listen to the entire firehose of every single bitcoin transaction and filter out only the ones you need, but even then you still need to parse every single raw transaction to see if it actually matches the condition you're looking for.
And guess what, once you've finished all that task, it's too late because you've already done all the work. It's an overkill for pretty much everyone who wants to build a bitcoin powered application.
This is especially true when you think about many ways this realtime pubsub can be used for, such as low power devices and simple apps that can't afford high push traffic or computing cycles.
This changes today.

Meet Bitsocket

Bitsocket lets you subscribe to any pattern of realtime bitcoin transactions through Bitquery to build realtime bitcoin event driven apps. Here's an example filter that listens to all realtime transactions and transforms them into a digestible API format:
But raw stream is boring. you can experience the real power when you filter it down to very precise pattern of transactions with Bitquery.
Here's me listening to a query for all incoming transactions to my address, then sending 0.001BCH to myself using Electron Cash, and bitsocket instantly pushing me the filtered transaction in realtime:
Note that in this picture, electron cash and bitsocket are NOT connected directly in any way, everything is pushed around via Bitcoin.
You can try this with your own wallet too. Go to the demo and replace the address with your own address, and send yourself some BCH. Then watch bitsocket
You can try some more examples as well:
OK that's pretty cool, but how does it actually work?

How it works

Step 1. Transform

Just like how BitDB transforms the raw linear bitcoin transaction format into a structured database format that can be queried, Bitsocket transforms every incoming bitcoin transaction into the same structured format, powered by the same module called TNA, in realtime. (You can learn more about TNA format here)

Step 2. Eventify

Unlike BitDB which takes the data from step 1 and stores it into MongoDB, Bitsocket instead runs a realtime in-memory filter and transform function to turn the transaction object into an application specific event for each subscribing client.
Note that each application plugged into bitsocket subscribes to a Bitquery object, instead of being dependent on a specific URL endpoint.
This content-addressed pubsub model (unlike traditional location-addressed models like a regular websockets app) is important. It means you can seamlessly switch out one bitsocket node to another and your app will function exactly the same because every bitsocket node is identical and your client is subscribed to the server-independent immutable query instead of being married to a specific server endpoint.

Step 3. Push

Each transformed event from the previous step is pushed to the corresponding application via SSE (Server Sent Events). The apps can be anything from a mobile app to a web app to a server side app to a hardware device.
And of course, these apps can in turn trigger another app, which means anything is possible.
It's important to note that, with this push paradigm, the very concept of what it means to "build an app on bitcoin" changes. Instead of "I'm adding a bitcoin support for my app", it becomes "I'm installing my app into the bitcoin computer."

How to use

I believe in simplicity and transparency, especially when it comes to bitcoin. This is why bitsocket is nothing more than just an SSE (Server Sent Events), a web standard. There's no library to use, you simply open a vanilla SSE connection to bitsocket and that's it.
Here's an example code for a fully functional application:
<html> <body> <script> var query = { "v": 3, "q": { "find": {} } } var b64 = btoa(JSON.stringify(query)) var bs = new EventSource(''+b64) bs.onmessage = function(e) { var str = JSON.stringify(JSON.parse(, null, 2) document.write("<pre>" + str + "</pre>") } </script> </body> </html>
That's literally it! Not even a 3rd party library. Bitsocket is automatically supported by ALL browsers as well as server side implementations like node.js
There is no special technology to learn because it's just Server Sent Events, all you need to learn is how to write the query so that you can express exactly the types of events you want to listen to. This query language is called Bitquery. and is the same query language that powers Bitdb.

What is possible?

Those of you who have used Bitdb may have already realized by this point, but bitsocket opens another dimension to bitcoin programming.
If bitdb were a "pull" based API, bitsocket is a "push" API for Bitcoin. With both pull and push, the combination of the two pretty much covers 100% of any application you can build on top of bitcoin.
Let's take a look at a few:

1. Realtime bitcoin apps

On the most primitive level, you can just think of bitsocket as the global event emitter of Bitcoin you can hook into. You can finally build flexible realtime bitcoin apps that use Bitcoin transactions as event.
With the expressive power of bitquery (the query language of bitsocket), you can build realtime bitcoin apps that:
  1. Listen to a certain transaction hash and trigger certain action (When my address receives money, provide the user with a unique link)
  2. Listen to a pattern of transaction that has certain push data at certain location (When there's a post for a topic "candy", dispense the candy from this vending machine)
  3. When certain pattern of transaction happens, send a push notification to an iOS or Android device.
  4. Listen to a pattern of transaction graph and act upon it (for exmple, "run code A if address B receives C amount from address D")
  5. and more (Bitquery is a Turing complete programmable query language so you can set up all kinds of filters)
In fact, this is how the explorer for Simple Ledger Protocol has been working behind the scenes from its launch, it's a server-side app that listens to incoming bitcoin events and updates its state based on every new socket event that dictates a state transition (I've extracted out bitsocket from tokengraph)
This means you too can build something like Tokengraph using bitsocket TODAY. That said, we're just getting started. There are other exciting types of apps you can build as well. Let's move on to the next section.

2. Trigger 3rd party API

Everything becomes clear if you just think of bitsocket as the global event emitter of the single source of truth that is bitcoin. You can build apps that listen to bitcoin events and trigger anything.
And this includes triggering a non-bitcoin 3rd party API such as Twitter, Github, Facebook, SMS, or you own enterprise API.
This can give rise to interesting combinations of various existing apps and bitcoin. Or existing businesses incorporating bitcoin into their workflow to deal with customers in more realtime manner.

3. Trigger bitcoin transaction

If we can trigger ANY logic from a bitsocket event, this begs the question: Why not use the event to trigger another event (bitcoin transaction)?
Yes! We can build applications that listen to certain event pattern from Bitcoin (with bitquery), and send a message back to bitcoin (in the form of a bitcoin transaction) when the conditions are met.
This way you can easily build sophisticated autonomous applications that loop back into and out of bitcoin whichever way you want.
I won't go further into details because I don't want to spoil all the fun for those of you developers who are already coming up with all the crazy ideas at this point. Some things are better left unsaid and shown through execution. I'll wait.

4. Bitcoin transactions as IPC (Inter Process Communication)

Let's go deeper.
Let's look at bitcoin as a computer, and bitcoin transactions as "events" that power all the programs plugged into bitcoin. This small change in perspective really opens doors to all kinds of potential.
First, we can think of these bitcoin applications as "external processes" that plug into the single global computer that is bitcoin.
These processes don't always have to be directly embedded in the computer. Just like the floppy disks and the cartridges of the early computer era, you can think of these apps as external devices that interface with the computer (bitcoin) to carry out certain tasks. Just like the floppy disks and cartridges, the algorithm for the program is stored externally but the actual computation happens on the CPU (bitcoin).
This means we can use bitcoin transactions as IPC (Inter Process Communications) of the Bitcoin computer by letting one app send events to another app in the form of Bitcoin transactions. Here's an example:
  1. App A is programmed to trigger Event B when there's an incoming Event A.
  2. App B is programmed to trigger Event C when there's an incoming Event B.
  3. App A is also programmed to run action X when there's an incoming Event C.
Now with that set up, let's imagine you trigger "Event A" by making a bitcoin transaction that matches the signature of "Event A".
  1. User triggers Event A by making a bitcoin transaction.
  2. App A is immediately notified by bitsocket of Event A, and triggers Event B.
  3. Bitsocket then delivers the Event B to App B.
  4. App B triggers Event C.
  5. App A, which was also listening to Event C, can confirm the entire roundtrip and finally run action X.

This is just one example but you get the idea.
This way it becomes possible to build a group of completely independent applications from multiple parties that autonomously talk to one another using Bitcoin as the central message bus and bitcoin transactions as inter process calls.

Bitsocket: Message bus for bitcoin apps

Bitcoin enables multiple independent agents (human or machine) to communicate with one another WITOUT having to know each other, trust each other, or play politics to get ahead. You just need to trust the blockchain, the universal source of truth.
But we still need to lower the barrier for the outside world to interact with bitcoin, because it doesn't matter how immutable a ledger is if it's not easy to read, filter, and process the data from the blockchain to carry out meaningful operations in the outside real world.
If Bitcoin were a computer, Bitdb would be the random access memory, and Bitsocket would be the message bus. And the goal for both projects is exactly to lower the barrier for the outside world to interact with bitcoin. You can literally build bitcoin apps in a couple of hours.
Excited yet? Let's build some apps!



14 of 15 reviewers say it's worth paying for

1 of 15 reviewers say it's not worth paying for

  earned 0.0¢
Incredible! You consistently impress me, thanks for all your contributions!
   1yr ago
  earned 0.0¢
Do you and your products or services have any connection to Calvin Ayre, CoinGeek, nChain or BSV Coin? Thanks for your answer. It is a great project and I am looking forward to being one of your biggest fans.
   1yr ago
  earned 95.0¢
@OpenMind I have zero connections with the outside world. Nobody even knows if I'm human
   1yr ago
10.0¢ 25.0¢ 10.0¢ 25.0¢ 24.9¢
  earned 0.0¢
super cool. i can already see uses for event listen. if you want to build any kind of website that takes payment you need to know when the tx comes in. Keep building; this is great!
   1yr ago
  earned 0.0¢
This looks very powerful, congratulations!
I assume events are triggered as soon as the transaction hits the mempool, correct? Have you thought about transactions that just show up in a block already, without appearing in your node's mempool before? Or even, are blocks and confirmations "events" of their own?
What happens if the bitsockets node goes offline and misses some transactions?
   1yr ago
  earned 50.0¢
I assume events are triggered as soon as the transaction hits the mempool, correct? Have you thought about transactions that just show up in a block already, without appearing in your node's mempool before? Or even, are blocks and confirmations "events" of their own?
Yes block events are included as well. Bitsocket emits events both on:
  1. New mempool transaction
  2. New block event

Each event object has an attribute called "type". In case of mempool it's "type": "mempool", and for block it's "type": "block".
The only difference is mempool event contains a single object whereas a block event contains an array of all the transactions from that block.

What happens if the bitsockets node goes offline and misses some transactions?
There are two different scenarios: If it's the app that goes offline (underground for example) and later come back online, it should magically work thanks to the magic of SSE.
However, if the bitsocket node itself goes down, the events will be lost because everything is ephemeral.
I thought about building a persistence layer into bitsocket itself, but after a long experimentation came to a conclusion that it's not worth the trouble for a couple of reasons:
  1. Just like any communication technologies, it's best to keep it simple and build additional layers on top of it (like how TCP adds a delivery guarantee layer on top of IP, etc.)
  2. A better way of handling this is if the app uses both BitDB AND Bitsocket. Bitsocket for most realtime processing, but you could also combine it with a constant polling of bitdb with the same bitquery, just to make sure you didn't miss anything. In case of BitDb it never misses anything because that's pretty much the point of the system (Even if it goes down, when it comes back up it will immediately catch up and synchronize to the latest tip)

I think there may be a way to abstract above two items into a higher level library so average users don't have to worry about handling these details, but so far I think this is the way to go.
Let me know if you have any feedback!
   1yr ago
25.0¢ 24.9¢
  earned 0.0¢
I don't understand any of the code, but I think I get your idea and I think its great. Good on ya'.
   1yr ago
  earned 0.0¢
Words can not express that humans have towards your obvious world changing work has, we can only hope your family understands this impact that we will all enjoy. Thank you and your love ones for such sacrifice.
   1yr ago