Reshuffle Connectors

Reshuffle is a framework that lets you connect to services, create integrations between systems, and build workflows using these services and systems.

Reshuffle achieves this by using Connectors as the connection mechanism to a 3rd party service or system. Each system Reshuffle connects to has their own connector. If you want to connect to Salesforce, for example, you would use the Salesforce connector.

Reshuffle ships with an existing ecosystem comprising several connectors you can use to connect to common systems. A list of these Reshuffle-provided connectors can be found in this page, and their code can be found in the various repositories here.

The framework is extendable, meaning you can create connectors to services you use or even your own proprietary systems. The process for building new connectors is described in the manual.

Common Reshuffle Connectors

Cron Connector

NPM Package: reshuffle

The Cron connector fires events at specified intervals. For example, you can use this connector to monitor a system every 10 seconds, or send a daily email report. In a single Reshuffle deployment, this connector can use the internal JS Timer, and in distributed setups you can connect it to a Cron service.

Here is an example of how you would send a daily email using this connector:

const {Reshuffle, CronConnector} = require('reshuffle')
const {SMTPConnector} = require('reshuffle-smtp-connector')

const app = new Reshuffle();
const cronConnector = new CronConnector(app);
const smtpConnector = new SMTPConnector(app,{
  username:'superman',
  password:'hunter123',
  host:'email.some.com',
  port: 587,
  fromName: 'Spiderman II',
  fromEmail: 'admin@superheros.com'
},'connectors/email');


cronConnector.on({'interval':86400000}, (event) => {
  event.getConnector('connectors/email')
    .send({
    to:'email@exmaple.com',
    subject: 'daily report!',
    html: 'The report itself'
    });
});

app.start()

Connector configuration

The Cron Connector does not require any configuration.

Connector events

Interval (default)

This event is emitted every preconfigured interval.

Interval Event configuration

the interval attribute in milliseconds.

{"interval":1000}

HTTP Connector

NPM Package: reshuffle

The HTTP connector is a wrapper to a web server (Express under the hood) it lets developers trigger logic when an HTTP endpoint is hit.

The HTTP Connector is not a full replacement for Express. If you are building a pure web application, without any integration requirements, then using pure Express might be a better decision. The HTTP connector and Reshuffle are a good fit when but if you are building an integrating system that needs HTTP integration then this connector will do the job.
building integrated flows that need to interact with other systems over HTTP.

The following example exposes an endpoint that changes your status in Slack:

const {HttpConnector, Reshuffle} = require('reshuffle');
const {SlackConnector} = require('reshuffle-slack-connector'); 

const app = new Reshuffle();
const httpConnector = new HttpConnector(app);
const slackConnector = new SlackConnector(app, {
  'authkey': process.env.SLACK_AUTH_KEY
}, 'connectors/Slack');

httpConnector.on({
  'method':'GET',
  'path':'/status'
}, (event) => {
  event.getConnector('connectors/Slack')
    .setStatus("U8675636646",event.req.query.slack_status); 
});

app.start();

The code above creates an HTTP endpoint at /status. Sending a GET request to this endpoint with the following pattern, sets the user status in Slack to the value of the slack_status query parameter. Ergo - hitting /status?slack_status=happy will set the user's status to happy.

SMTP Connector

NPM Package: reshuffle-smtp-connector

The SMTP connector allows a developer to configure a transport that sends emails via SMTP.

The following example exposes an endpoint that changes your status in Slack:

const {HttpConnector, Reshuffle} = require('reshuffle');
const {SMTPConnector} = require('reshuffle-smtp-connector')

const app = new Reshuffle();
const httpConnector = new HttpConnector(app);
const smtpConnector = new SMTPConnector(app, {
  username:'superman',
  password:'hunter123',
  host:'email.some.com',
  port: 587,
  fromName: 'Spiderman III',
  fromEmail: 'admin@superheros.com'
},'connectors/smtp');

httpConnector.on({
  'method':'GET',
  'path':'/ping'
}, (event) => {
  event.getConnector('connectors/smtp')
    .send({
      to:event.req.query.to,
      subject: 'Ping Email',
      html: 'You have been pinged'
    }); 
});

app.start()

The code above creates an HTTP endpoint at /ping. Sending a GET request to this endpoint with the following pattern, sends an email to the address in the to query parameter.

Ergo - hitting /ping?to=doc@exmaple.com will send a ping email to doc@example.com

AWS S3 Connector

NPM Package: reshuffle-aws-connectors

This connector can be used to manage AWS S3 buckets and objects.

The following example logs when a new object is added to a S3 bucket:

const { Reshuffle } = require('reshuffle')
const { AWSS3Connector } = require('reshuffle-aws-connectors')

const app = new Reshuffle()

const s3Connector = new AWSS3Connector(app, {
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  bucket: process.env.AWS_BUCKET,
})

s3Connector.on({ type: 'ObjectAdded' }, async (event) => {
  console.log(event)
})

app.start(8000)

AWS Elastic Transcoder Connector

NPM Package: reshuffle-aws-connectors

This connector can be used to transcode video and audio using Amazon's transcoder service.

The following example uses the AWS Elastic Transcoder

const app = new Reshuffle()

  const awsElasticTranscoder = new AWSElasticTranscoderConnector(app, {
    accessKeyId: process.env.SECRETS_AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.SECRETS_AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION,
  })

  const httpConnector = new HttpConnector(app)

  const pipeline = await awsElasticTranscoder.findPipelineByName(
    process.env.ELASTIC_TRANSCODER_PIPELINE,
  )
  const preset = await awsElasticTranscoder.findPresetByDescription('240')

  httpConnector.on({ method: 'GET', path:'/go' }, async (event) => {
    const output = `video-${Date.now().toString(16)}.mp4`

    const job = await awsElasticTranscoder.createJob({
      PipelineId: pipeline.Id,
      Input: {
        Key: 'video.mov', // replace with actual filename
      },
      Output: {
        PresetId: preset.Id,
        Key: output,
        Rotate: '180',
      },
    })

    return event.context.res.json({ jobId: job.Id, output })
  })

  awsElasticTranscoder.on({ pipelineId: pipeline.Id }, async (event) => {
    console.log(`Transcoding job progress ${event.jobId}: ${
      event.old.Status} -> ${event.current.Status}`)
  })

  app.start(8000)

AWS Lambda Connector

NPM Package: reshuffle-aws-connectors

This connector can be used to access AWS Lambda. It is implemented using Amazon's Lambda SDK.

The following example deploys and invokes a lambda function:

const awsLambdaConnector = new AWSLambdaConnector(app, {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION,
  })

  const funcName = `function-${crypto.randomBytes(8).toString('hex')}`

  console.log('Deploying Lambda function:', funcName)
  await awsLambdaConnector.createFromCode(funcName, `
    exports.handler = async (event) => {
      const a = event.a || 0
      const b = event.b || 0
      const response = {
        sum: a + b,
        prod: a * b,
      }
      return {
        statusCode: 200,
        body: JSON.stringify(response),
      }
    }
  `)

  const req = { a: 3, b: 5 }
  const res = await awsLambdaConnector.invoke(funcName, req)
  console.log('Lambda response:', req, '->', res)

  console.log('Deleting Lambda function')
  await awsLambdaConnector.delete(funcName)

IMAP Connector

NPM Package: reshuffle-imap-connectors

This connector provides the Reshuffle framework with IMAP email fetching capabilities.

The following example listens to an incoming email:

const { Reshuffle } = require('reshuffle')
const { IMAPConnector } = require ('reshuffle-imap-connector')

// Can easily be tested using https://ethereal.email/

const app = new Reshuffle()
const imap = new IMAPConnector(app,
  {
    host: '<imap host>',
    port: 993,
    user: '<inbox email address>',
    password: '<inbox password>',
    tls: true,
   // tlsOptions: Record<string, any>
    markSeen: false,
  },
  'connectors/IMAP'
)

imap.on({name:'email'},'email',(event) => {
    console.log(event.mail.body.text)
})

app.start()

Twilio Connector

NPM Package: reshuffle-twilio-connectors

This connector can be used to send SMS and MMS out. It can also trigger handlers on incoming messages.

The following example listens to an incoming SMS and reply:

const { Reshuffle } = require('reshuffle')
const { TwilioConnector } = require('reshuffle-twilio-connector')
const app = new Reshuffle()

// Create w Twilio account in few minutes for free: https://www.twilio.com/
const twilioConnector = new TwilioConnector(
  app, {
    accountSid: '<accountSid>',
    authToken: '<authToken>',
    twilioNumber: '<twilioNumber>'
  });

// Reply to an incoming message on <twilioNumber>
twilioConnector.on({method:'POST', path:'/sms'}, (event) => {
  const messageReceived = event.context.req.body.Body
  const fromPhoneNumber = event.context.req.body.From
  console.log(`New SMS received from ${fromPhoneNumber}: ${messageReceived}`)

  if(messageReceived.includes('test')) {
    event.context.res.end("test successful")
  } else {
    event.context.res.end("Thanks for your message")
  }
})

app.start()