subscribeNotifications
Subscribe to receive event notifications from a contract.
HTML
<script src="https://cdn.jsdelivr.net/npm/@cityofzion/neon-js/dist/browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@cityofzion/neon-js/dist/browser.min.js"></script>
<button id="go" onclick="InvokeSubscribeOperation();">
Subscribe
</button>
<button id="go" onclick="InvokeUnsubscribeOperation();">
Unsubscribe
</button>
<p>
</p>
<div id="result">
Press button above to run the example.
</div>
<p>
</p>
CSS
body {
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif;
font-size: 16px;
line-height: 1.5;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
word-wrap: break-word;
}
button {
font-size: 16px;
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
background: rgb(66, 184, 221); /* this is a light blue */
}
JavaScript
// ScriptHash of the contract to subscribe to
const contractHash = "0x314b5aac1cdd01d10661b00886197f2194c3c89b";
// Subscriptions to null are equivalent to subscribing to all the contracts
const allContractHashes = null;
// Get an instance of the Notifications server
const provider = new Neon.api.notifications.instance("wss://YOUR_PUBSUB_SERVER.com/event");
let subscription;
function InvokeSubscribeOperation()
{
clearHtml();
// Create a subscription
subscription = provider.subscribe(allContractHashes, (event) => {
outputHtml('Event data: ');
outputHtml(JSON.stringify(event, null, " "));
});
}
function InvokeUnsubscribeOperation()
{
if(subscription !== undefined){
// Unsubscribe the previous handler
subscription.unsubscribe();
}
}
// Utility function to print output to an HTML div tag
function outputHtml(s) {
document.getElementById("result").innerHTML += s + "<br>";
}
function clearHtml() {
document.getElementById("result").innerHTML = "";
}