Samples

This article contains four sample Node.js programs demonstrating message consumption and emission for both Kaleido-managed destination certificates and externally-managed destination certificates.

Kaleido-Managed Sender

Replace the placeholders for APP2APP MESSAGING SERVICE API ENDPOINT, APP CRED USER, APP CRED PASSWORD, SENDER DESTINATION URI and RECIPIENT DESTINATION URI.

// This sample sends a message every two seconds from a Kaleido managed destination
const socket = require('socket.io-client').connect('APP2APP MESSAGING SERVICE API ENDPOINT', {
    extraHeaders: {
      Authorization: 'Basic ' + Buffer.from('APP CRED USER' + ':' + 'APP CRED PASSWORD').toString('base64')
    }
  })
  .on('connect', () => {
    console.log('Connected.');
    setInterval(send, 2000);
  }).on('delivery-report', data => {
    console.log('Delivery report: ' + JSON.stringify(data));
  }).on('disconnect', () => {
    console.log('Disconnected.');
  }).on('exception', exception => {
    console.log('Exception: ' + exception);
  }).on('error', err => {
    console.log('Error: ' + err);
  }).on('connect_error', err => {
    console.log('Connection error: ' + err);
  });

function send() {
  socket.emit('produce', {
      headers: {
        from: 'SENDER DESTINATION URI',
        to: 'RECEPIENT DESTINATION URI',
      },
      content: 'Testing 1-2-3'
    },
    'samplekey',
    err => {
      if (err) {
        console.log('Delivery error: ' + err);
      }
    });
}

Kaleido-Managed Receiver

Replace the placeholders for APP2APP MESSAGING SERVICE API ENDPOINT, APP CREDENTIAL USER, APP CREDENTIAL PASSWORD and DESTINATION URI.

// This sample consumes messages from a Kaleido managed destination
const socket = require('socket.io-client').connect('APP2APP MESSAGING SERVICE API ENDPOINT', {
    extraHeaders: {
      Authorization: 'Basic ' + Buffer.from('APP CREDENTIAL USER' + ':' + 'APP CREDENTIAL PASSWORD').toString('base64')
    }
  })
  .on('connect', () => {
    console.log('Connected.')
    socket.emit('subscribe', ['DESTINATION URI OR NAME'], (err, result) => {
      if (err) {
        console.log(err)
      } else {
        console.log(result)
      }
    });
  }).on('disconnect', () => {
    console.log('Disconnected.');
  }).on('exception', exception => {
    console.log('Exception: ' + exception);
  }).on('error', err => {
    console.log('Error: ' + err);
  }).on('connect_error', err => {
    console.log('Connection error: ' + err);
  }).on('data', (message, key, timestamp) => {
    console.log('Message from: ' + message.headers.from);
    console.log('Content: ' + message.content);
    console.log('key: ' + key);
    console.log('timestamp: ' + timestamp);
  });

Externally-Managed Sender

Replace the placeholders for APP2APP MESSAGING SERVICE API ENDPOINT, APP CRED USER, APP CRED PASSWORD, PRIVATE KEY PEM, SENDER DESTINATION URI and RECIPIENT DESTINATION URI

// This sample sends a message every two seconds from an externally managed destination
const socket = require('socket.io-client').connect('APP2APP MESSAGING SERVICE API ENDPOINT', {
    extraHeaders: {
      Authorization: 'Basic ' + Buffer.from('APP CRED USER' + ':' + 'APP CRED PASSWORD').toString('base64')
    }
  })
  .on('connect', () => {
    console.log('Connected.');
    setInterval(send, 2000);
  }).on('delivery-report', data => {
    console.log('Delivery report: ' + JSON.stringify(data));
  }).on('disconnect', () => {
    console.log('Client disconnected.');
  }).on('exception', exception => {
    console.log('Service reported an exception: ' + exception);
  }).on('error', err => {
    console.log('Error: ' + err);
  }).on('connect_error', err => {
    console.log('Connection error: ' + err);
  });

function send() {
  let content = 'Testing 1-2-3';
  let signature = require('crypto').createSign('RSA-SHA256').update(content).sign('PRIVATE KEY PEM');
  socket.emit('produce', {
      headers: {
        from: 'SENDER DESTINATION URI',
        to: 'RECEPIENT DESTINATION URI',
        signature
      },
      content
    },
    'samplekey',
    err => {
      if (err) {
        console.log('Delivery error: ' + err);
      }
    });
}

Externally-Managed Receiver

Replace the placeholders for APP2APP MESSAGING SERVICE API ENDPOINT, APP CREDENTIAL USER, APP CREDENTIAL PASSWORD, RECIPIENT DESTINATION URI, RECIPIENT PRIVATE KEY PEM and SENDER CERTIFICATE PEM.

// This sample consumes messages from a Kaleido managed destination
const socket = require('socket.io-client').connect('APP2APP MESSAGING SERVICE API ENDPOINT', {
    extraHeaders: {
      Authorization: 'Basic ' + Buffer.from('APP CREDENTIAL USER' + ':' + 'APP CREDENTIAL PASSWORD').toString('base64')
    }
  })
  .on('connect', () => {
    console.log('Connected.');
    socket.emit('subscribe', ['RECEPIENT DESTINATION URI OR NAME'], (err, result) => {
      if (err) {
        console.log(err);
      } else {
        console.log(result);
      }
    });
  }).on('disconnect', () => {
    console.log('Disconnected.');
  }).on('exception', exception => {
    console.log('Exception: ' + exception);
  }).on('error', err => {
    console.log('Error: ' + err);
  }).on('connect_error', err => {
    console.log('Connection error: ' + err);
  }).on('data', (pkcs7Envelope, key, timestamp) => {

    const forge = require('node-forge');
    const pkcs7 = forge.pkcs7.messageFromPem(pkcs7Envelope);
    pkcs7.decrypt(pkcs7.recipients[0], forge.pki.privateKeyFromPem('RECEPIENT PRIVATE KEY PEM'));
    const message = JSON.parse(pkcs7.content.toString());
    console.log('Message: ' + message.content);
    console.log('Key: ' + key + ', timestamp: ' + timestamp);
    console.log('Signature checks out: ' +
      require('crypto').createVerify('RSA-SHA256')
      .update(message.content)
      .verify('SENDER CERTIFICATE PEM OBTAINED FROM ID REGISTRY BASED ON SENDER DESTINATION URI',
        Buffer.from(message.headers.signature)));
  });