Calling Your Backend


We recommend using Zenaton in its own project. This is the easiest way to maintain it and quickly improve your workflows. Nevertheless, in some cases, you may want to process some works in your backend.

There are no limitation to the code of Zenaton tasks, so you can choose your preferred way to handle this, for example:

  • by doing synchronous http call to your internal API
  • by putting a message in a queue listen by your workers
  • by doing synchronous gRPC call to your microservice

Example of a Zenaton task using an http request (add your authentification mechanism if needed):

const axios = require("axios");

module.exports.handle = async function() {
  return axios.get("https://www.mybackend.com/api/articles");
});

Example of a Zenaton task using a rabbitmq queue:

const amqplib = require("amqplib");

module.exports.handle = async function() {
  const connection = await amqplib.connect("amqp://localhost");
  const channel = await connection.createChannel();
  const queue = await channel.assertQueue("messages");
  await channel.sendToQueue("messages", JSON.stringify({ hello: "zenaton" }));
  await channel.close();
  await connection.close();
};

Example of a Zenaton task using gRPC:

const grpc = require('grpc');
const protoLoader = require('@grpc/proto-loader');

const PROTO_PATH = __dirname + '/../protos/helloworld.proto';

module.exports.handle = function(name) {
  const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  });

  const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
  const client = new helloProto.Greeter('localhost:50051', grpc.credentials.createInsecure());

  return new Promise((resolve, reject) => {
    client.sayHello({ name }, (err, response) => {
      if (err) {
        reject(err);
      } else {
        resolve(response);
      }
    });
  });
};