Actyx-File-Connector wanted

We need a connector to communicate with an Actyx swarm from external applications via a file.
We want to trigger Actyx events as soon as the input file arrives in a monitored directory.
Is there an Actyx file connector or something similar that we can use for these types of scenarios?

Hello @Mugdin,

It’s simple to create a node application that watches a certain folder. I attached a sample that watches a folder and emits an event whenever a file is added, changed or deleted.

While plain NodeJS also supports file watching, the results look different on different OSes, so we use the chokidar library which deals with platform differences.

This is how the sample looks in action.
file-watcher

Here’s the code, and the complete project: 189-file-watcher.zip (73.3 KB)

To test it, unzip the sample and run npm install && npm run node:file-watcher-start. Then change files in the data subfolder of the project. Currently, it fires an added event for each file already present in the folder. To prevent that, add ignoreInitial: true to the watcher options as shown below. It depends on your use case if you actually want that or not.

import { Pond, Tags } from '@actyx/pond'
import manifest from './manifest'
import chokidar from 'chokidar'

Pond.default(manifest).then((pond) => {
  const observedFolder = 'data'
  console.log(`Watching for file changes in ${process.cwd()}/${observedFolder}`)

  const watcher = chokidar.watch(`./${observedFolder}`, {
    ignored: /(^|[\/\\])\../, // ignore dotfiles
    persistent: true,
    ignoreInitial: true // don't fire events for files already present
  });

  watcher
    .on('add', path => pond.publish(
      Tags('file', 'file.added').apply([{ path, action: 'added' }])
    ))
    .on('change', path => pond.publish(
      Tags('file', 'file.changed').apply([{ path, action: 'changed' }])
    ))
    .on('unlink', path => pond.publish(
      Tags('file', 'file.deleted').apply([{ path, action: 'deleted' }])
    ));
})

@wwerner that looks very good! The recommendation of the chokidar library was perfect.Thanks a lot :+1:

Good to hear.
If it solves your problem, please mark the post as solved so others find the solution (well, that actually only is a problem if the thread is longer) and we don’t loose track on which questions have been resolved.
Thanks!