Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codefresh-io/cf-queue

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codefresh-io/cf-queue

Queue wrapper for nats.io

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
133
decreased by-57.91%
Maintainers
0
Weekly downloads
 
Created
Source

cf-queue - Queue wrapper for nats.io server

Creating a queue reference

var Queue   = require('cf-queue');

var options = {
  servers: [
    'nats://localhost:4222'
  ]
};

var queue = new Queue('queue-name', options);

The same initalization code is used by the clien and worker sides.

How to use it

Client side

When a client would like to push request into the queue it should use the queue request function and pass the request object.

var request = {
  id: 1,
  message: 'hello'
}

queue.request(request)
  .then(function(response) {
    console.log(response);
  })
  .catch(function(err) {
      console.error(err);
  });

Worker side

queue.process(function(job, callback) {
  var request = job.request;
  
  var response = {
    id: request.id,
    message: request.message + ' world !!!'
  };
  
  callback(null, response);
}

As first parameter for the callback you can return error message or object that will be sent back to the client as error.

Progress

Client side

When a client would like to push request into the queue it should use the queue request function and pass the request object.

var request = {
  id: 1,
  message: 'hello'
}

queue.request(request)
  .then(function(response) {
    console.log(response);
  })
  .progress(function(message) {
    console.log('PROGRESS >>> ' + message);
  })
  .catch(function(err) {
      console.error(err);
  });

Worker side

queue.process(function(job, callback) {
  var request = job.request;
  
  var response = {
    id: request.id,
    message: request.message + ' world !!!'
  };
  
  var counter = 0;
  var sendProgress = function() {
    counter++;
    job.progress('Step ' + counter + ' in ' + request.id);
  
    if (counter === 5) {
      callback(null, response);
    } else {
      setTimeout(sendProgress, 500);
    }
  };
  sendProgress();
}

Docker

You can see example of how to use the queue in Docker in the attached Dockerfile and docker compose file. The docker compose file load one client, two workers and the queue server.

Keywords

FAQs

Package last updated on 04 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc