Sending and Receiving with the Javascript driver

This tutorial uses the Stream Machine NodeJS driver, and the corresponding examples to send some data.

Getting started

Clone the examples and cd into the directory.

Create a stream, with associated credentials. Store the credentials in assets/credentials.json [1]

Run npm i to install dependencies.

Sending data

Start sending data.

$ npm run sender

If everything goes well you see precisely nothing! Looking at the code src/sender.ts we see the line if (r.status !== 204) { that is causing this silence.

This example code uses the Avro schema definition assets/clickstream.avsc and a library named avsc to generate Avro binary data. Below we’ll see another example that uses generated Typescript classes.

Receiving data

The example receives data over the websocket connection, whose working is defined in the low level docs.

$ npm run receiver
{
  "strmMeta": {
    "schemaId": "clickstream",
    "nonce": -518006066,
    "timestamp": 1616686102833,
    "keyLink": 1594186260,
    "billingId": "hello0123456789",
    "consentLevels": [ 0, 1, 2 ]
  },
  "producerSessionId": "ARq+8rE+J2sSZa7PLgExr0MrjDiB3nUEfaiGRDGo",
  "url": "https://portal.streammachine.io/",
  "eventType": "button x clicked",
  "referrer": "https://www.streammachine.io",
  "userAgent": "node-js",
  "conversion": 1,
  "customer": { "id": "ARq+8rHkrtvCoZwpo8IZdPsCHymWfgMQ5EkTusebIoM=" },
  "abTests": [ "abc" ]
}
...

Sending data with generated Typescript classes

Another example uses generated Typescript classes. This code is similar except that the Avro schema is not used directly, but the generated Typescript classes are. These are generated using @chasdevs/avro-to-typescript.

In order to make this demo work, you can re-use the assets/credentials.json from the previous example, but here you have to store it in the top directory of the cloned repo.

$ npm i
$ npm run sender

> node-js-example@0.0.1 sender
> ts-node ./src/sender.ts

Starting with configuration ../credentials.json
connecting to undefined, https://in.strm.services/event
Sending an event every 100ms for 1800s.

1. use strm auth access-token to get your billing id