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 https://bitsocket.org/channel/ewogICJ2IjogMywKICAicSI6IHsKICAgICJmaW5kIjogewogICAgICAib3V0LmUuYSI6ICJxcTRrcDN3M3loaHZ5NGdtNGpnZXphNHZ1czh2cHhncndjOTBuOHJoeGUiCiAgICB9CiAgfQp9 and replace the address with your own address, and send yourself some BCH. Then watch bitsocket

You can try some more examples as well: https://bitsocket.org/examples

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(' https://bitsocket.org/s/ '+b64) bs.onmessage = function(e) { var str = JSON.stringify(JSON.parse(e.data), 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 .

Here's another blog post about how bitquery works: https://www.yours.org/content/bitquery---a-turing-complete-bitcoin-query-language-for-building-immut-faf4bb8477db





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:

Listen to a certain transaction hash and trigger certain action (When my address receives money, provide the user with a unique link) Listen to a pattern of transaction that has certain push data at certain location (When there's a memo.cash post for a topic "candy", dispense the candy from this vending machine) When certain pattern of transaction happens, send a push notification to an iOS or Android device. 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") 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 https://tokengraph.network 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:

App A is programmed to trigger Event B when there's an incoming Event A. App B is programmed to trigger Event C when there's an incoming Event B. 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".

User triggers Event A by making a bitcoin transaction. App A is immediately notified by bitsocket of Event A, and triggers Event B. Bitsocket then delivers the Event B to App B. App B triggers Event C. 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!

Go check out some examples: https://www.bitsocket.org/examples

Try the quickstart: https://www.bitsocket.org/docs

Follow me on Twitter for announcements: https://twitter.com/_unwriter

Join the chat to ask questions: https://t.me/joinchat/HH1DDQ8pZlSlsdNcKgIcxw