Complete examples

This page aims to provide full examples, from sending data to Stream Machine to receiving the data through the Egress.

These examples are meant for demonstration purposes and use the Egress interface to receive events. For production ready interfaces, we refer to the Batch and Kafka exporters.

In order to run these examples, you need the following:

  • An input stream to send data to (if you don’t know how, click here to learn how to create streams)

  • The credentials for this stream (presented upon creation)

  • The billing id for your account (you can find your billing id via the CLI or in the portal)

Please see the readme of the examples presented below on how to provide the credentials to the example code.

Sending data

The following demo application displays how dummy data can be sent with a certain frequency. The data that is sent is quite static and does not result in any useful patterns for analysis, however, it does show how data can be constructed and tranferred to Stream Machine.

  • Java

  • Python

  • NodeJS

java driver clickstream

This example is also available on GitHub. Please see the repository for the readme.

404: Not Found

streammachine driver streammachine schemas clickstream avro

This example is also available on GitHub. Please see the repository for the readme.

404: Not Found

nodejs driver

This example is also available on GitHub. Please see the repository for the readme.

import { Sender } from '@strmprivacy/nodejs-driver';
import { DemoEvent } from '@strmprivacy/schemas-demo-avro';

const CONFIG = require('../assets/config.json');

// Copy the template credentials file, name it `credentials.json` and fill out the values
const CREDENTIALS = require('../assets/credentials.json');

const startSender = async () => {
  // Note: the schema id is hard coded in this example, in the config.json. This will be dynamically determined in a future version
  const sender = new Sender({
    ...CONFIG,
    ...CREDENTIALS,
  });

  // Make sure to listen for error events, otherwise Node does not handle the error events (they're escalated)
  sender.on('error', (error) => {
    console.log('Sender', error);
  });

  await sender.connect().catch((e) => {
    console.error(`Connect error ${e}`, e);
  });

  setInterval(async function () {
    try {
      const r = await sender.send(createEvent(), 'AVRO_BINARY');

      console.log(`Status ${r.status}`);
      if (r.status !== 204) {
        console.error(`An error occurred while sending event:`, r);
      }
    } catch (e) {
      console.error(`Error: ${e.message}`, e);
    }
  }, 100);
};

const createEvent = () => {
  const event = new DemoEvent();
  event.strmMeta = {
    eventContractRef: 'strmprivacy/example/1.2.3',
    consentLevels: [0],
  };

  event.uniqueIdentifier = 'string';
  event.someSensitiveValue = 'A value that should be encrypted';
  event.consistentValue = 'a-user-session';
  event.notSensitiveValue = 'Hello from NodeJS';

  return event;
};

startSender();

Receiving data

And the application shown below, displays how the data can be received through the Egress, which is transferred over Websocket.

  • Java

  • Python

  • NodeJS

java driver clickstream

This example is also available on GitHub. Please see the repository for the readme.

404: Not Found

streammachine driver streammachine schemas clickstream avro

This example is also available on GitHub. Please see the repository for the readme.

import asyncio
import json
import logging
import sys

from args import StrmPrivacyProperties
from client_builder import ClientBuilder


async def event_handler(event):
    """
    callback handler for the events from STRM Privacy

    :param event: this is either json event, or a base64 encoded avro binary, depending on the
    first parameter of StrmPrivacyClient.start_receiving_sse.

    Decoding an Avro binary is not yet supported.
    """
    print(json.loads(event))


async def main(props):
    """
    Your async main code that instantiates the client, starts its re-authorization timer, and installs a callback
    """
    client = ClientBuilder.create_strm_privacy_client()
    await client.start_timers()
    await client.start_receiving_ws(True, event_handler)


if __name__ == '__main__':
    logging.basicConfig(stream=sys.stderr)
    asyncio.run(main(StrmPrivacyProperties.from_args()))

nodejs driver

This example is also available on GitHub. Please see the repository for the readme.

import { Receiver } from '@strmprivacy/nodejs-driver';

const CONFIG = require('../assets/config.json');
const CREDENTIALS = require('../assets/credentials.json');

async function startReceiver() {
  const receiver = new Receiver({
    ...CREDENTIALS,
    ...CONFIG,
  });

  receiver.on('event', (event) => {
    console.log(JSON.stringify(event));
  });

  receiver.on('error', (error) => {
    console.log('Error', error);
  });

  try {
    await receiver.connect();
  } catch (error) {
    console.log('Connect failed', error);
  }
}

startReceiver();

Configuration properties

The default configuration properties are shown below. If necessary or requested, you can provide the properties to your Stream Machine client.

Property Value

Gateway Scheme

https

Gateway Port

443

Gateway Host

in.strm.services

Gateway Endpoint

/event

Egress Scheme

https

Egress Host

out.strm.services

Egress Endpoint

/ws

Authentication Scheme

https

Authentication Host

auth.strm.services

Authentication Endpoint

/auth

Authentication Refresh Endpoint

/refresh