Skip to content

Basic Produce

Jeremy Barlow edited this page Aug 7, 2018 · 1 revision

This sample demonstrates how to produce records to the DXL streaming service.

The majority of the sample code is shown below:

Sample Code

var CHANNEL_TOPIC = 'my-topic'

// Create the message payload to be included in a record
var messagePayload = {
  message: 'Hello from OpenDXL'
}

// Create the full payload with records to produce to the channel
var channelPayload = {
  records: [
    {
      routingData: {
        topic: CHANNEL_TOPIC,
        shardingKey: ''
      },
      message: {
        headers: {},
        // Convert the message payload from an object to a base64-encoded
        // string.
        payload: Buffer.from(JSON.stringify(messagePayload)).toString('base64')
      }
    }
  ]
}

// Create a new channel object
var channel = new Channel(CHANNEL_URL,
  addTlsOptions({
    auth: new ChannelAuth(CHANNEL_URL, CHANNEL_USERNAME,
      CHANNEL_PASSWORD, addTlsOptions())
  })
)

// Produce the payload records to the channel
channel.produce(
  channelPayload,
  function (error) {
    channel.destroy()
    if (error) {
      console.log('Error : ' + error)
    } else {
      console.log('Succeeded.')
    }
  }
)

The first step is to create a payload object which includes an array of records to be sent to the channel. The message.payload item in each record is flattened from a dictionary into a string and encoded using the base64 algorithm.

The next step is to create a Channel instance, which establishes a channel to the streaming service. The channel parameters include the URL to the streaming service, CHANNEL_URL, and credentials that the client uses to authenticate itself to the service, CHANNEL_USERNAME and CHANNEL_PASSWORD.

The final step is to call the produce() method with the payload of records to be produced to the channel. Assuming the records can be produced successfully, the text "Succeeded." should appear in the console output.

Output

If the records are successfully produced to the streaming service, the following line should appear in the output window:

Succeeded.

Home

OpenDXL Streaming JavaScript Client Library

SDK Classes

Examples

Clone this wiki locally