Events

Real time transaction notification

To create a client application that receives real-time notifications for transactions (BSV, Token, and NFT), we will use the socket.io-client package. This document provides a step-by-step guide for implementing this functionality.

Step 1: User Authentication

The first step is to authenticate the user by making a request to the "/v1/login" endpoint. This is a necessary step to ensure that only authorized users can receive transaction notifications.

Step 2: Event Listening

The next step is to set up the client application to listen for specific events in order to receive relevant messages and data. The following events can be listened for:

  • "notification": To receive transactional notifications.

  • "balance": To receive updates on balance changes.

  • "history": To receive updates on history changes.

Here is an example of how to implement event listening in your client application:

const { io } = require('socket.io-client')
            
const relysiaEndpoint = 'https://api.enterprise.relysia.com/';

(async () => {
    // Connect websocket for this account
    const socket = io(relysiaEndpoint, {
        auth: {
            serviceid: serviceid
            authToken: token
        }
    });

    socket.on('error', function(error) {
        console.log("Connection Error: " + error.toString());
    });

    // Listen for message and log them as notification arrive
    socket.on('notification', function (message) {
        console.log('Notification received:', message);
    })

    // Listen for balance and log them on balance arrive
    socket.on('balance', function (balance) {
        console.log('Balance update received:', balance);
    })

    // Listen for history and log them on history arrive
    socket.on('history', function (history) {
        console.log('History update received:', history);
    })
    
    socket.on('connect', function(connection) {
        console.log('WebSocket Client Connected');
        socket.on('close', function() {
            console.log('Connection Closed');
        });
    });
})()

Last updated