Broadcast Ethereum Events with Web3.js and PubNub

3 min read Adam Bavosa on Mar 22, 2018

In this post, we’ll show you how to harness the power of PubNub to amplify Ethereum smart contract events. Using our global, real-time network APIs, we’ll intelligently update the crowd watching your decentralized application.

What is Ethereum?

Ethereum is a decentralized blockchain network for running smart contracts. Let’s say you’ve created your DApp masterpiece, but you need a way to signal the execution of events to your constituency. An example of this would be a network of investors that are awaiting the opening of your Cryptotoken crowdsale. It’s time to use the PubNub megaphone to tell everyone that your crowdsale has opened, in real time.


Web3.js is the JavaScript framework for interfacing with blockchain smart contracts. Whether you have a node.js app, or a web browser front end, web3.js has 2 core purposes:

  • Trigger the execution of smart contract functions
  • Listen to events (like solidity events) that are signaled when a contract executes

The Crowdsale Has Opened!

Using the Crowdsalable Ethereum Token code we can write a node.js app to listen for the Ethereum events and update all of our fans using PubNub.

PubNub is a real-time, global, data delivery network. We can make sure that all of our crowdsale investors, who may be staring at a purchase webpage while holding their breath, see that the sale has opened in an instant. PubNub can be used to keep a massive load of users in-sync, send real-time updates, and also perform data enrichment.

Supercharge Your Smart Contract Events

An app that is listening to Ethereum events, that is also connected to PubNub, can trigger mobile push notifications, send text messages, send emails, and much more. Using Mobile Push , or Functions, tremendous capability is added to mere Ethereum events.

The Open Growth SDK uses Functions to collect user data with Clearbit, classify a customer’s use case with a machine learning API, and then submit an enriched alert. All of this functionality is triggered by one PubNub message. In this similar use case, PubNub can make your smart contract events powerful.

Supercharge Your Smart Contract Events

Here is the Node.js script that will commit the publish to PubNub’s network, which guarantees delivery in under a quarter of a second to your millions of patient investors.

You can check out the full GitHub Repo for this project.

// Libs
const PubNub = require('pubnub');
const Web3 = require('web3');
const ContractBuild = require('PATH_TO_TRUFFLE/build/contracts/Token.json');
// PubNub
pubnub = new PubNub({
  publishKey : '__YOUR_PUB_KEY__',
  subscribeKey : '__YOUR_SUB_KEY__'
function publishMessage(event) {
  let publishConfig = {
    channel : 'new_crowdsales',
    message : event
  pubnub.publish(publishConfig, function(status, response) {
    console.log(status, response);
// You can subscribe on a webpage that allows the clients to purchase the token.
// The instant the crowdsale opens they can see it in their browser thanks to PubNub:
  message: function(message) {
    console.log("New Message!!", message);
  channels: ['new_crowdsales'] 
// Web3.js
let web3;
if (typeof web3 !== 'undefined') {
  web3 = new Web3(web3.currentProvider);
} else {
  // set the provider you want from Web3.providers
  // this is the default `truffle develop` network
  // it can of course be changed to the main Ethereum network
  web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:9545"));
web3.eth.defaultAccount = web3.eth.accounts[0];
let TokenContractABI = web3.eth.contract(ContractBuild.abi);
let TokenContract ='__YOUR_CONTRACT_ADDRESS__');
let crowdsaleDeployedEvent = TokenContract.CrowdsaleDeployed();, result){

In order to get this example running, you need to input the path of the JSON file for your smart contract build in line 4. Next paste in your PubNub API keys, and finally your contract’s address toward the bottom of the file. This code can be used in conjunction with the Crowdsalabe Token project mentioned earlier.

This connector opens the doors for Functions and Mobile Push Notifications to ensure that our constituency gets a text, email, push notification, and we can post to a Twitter account, all in an automated fashion.