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

nthread-js

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nthread-js

Create easily threads in NodeJs

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Nthread JS

Create easily threads in NodeJs. The purpose is to delegate some part of your code in own processus.
It means that the code is executed in another process, thread, and give you the possibility to communicate with the main thread.

⚠ Be careful ⚠
Don't try to make an infinite loop during the creation of the thread.
Obviously, your server/computer will be affected by the number of threads.

☁️ Installation

$ npm install nthread-js

⚙️ Examples

const { listen } = require('nthread-js');

console.log("[root] - Run listen");

listen() // listen(string: port = random port, object: options = undefined)
  .then(async nthread => {
    console.log("[root] - Server connected");
    console.log("[root] - public uri", nthread.getPublicUri());
    console.log("[root] - local uri", nthread.getLocalUri());

    nthread.response(data => { // retrieve all communication
      console.log('[root] - message read from nthread : "', data.content, '" by "', data.guuid, '"');
      nthread.getByGuuid(data.guuid).send("Welcome from root =)");
    });

    // ---
    const child = await nthread.create(thread => { // create new thread
      // CODE EXECUTED IN HIS OWN THREAD
      thread.log('[child] - Child is now connected with PID: ' + thread.getPid()); // log a message to the main thread

      thread.response(content => { // retrieve communication from main thread
        thread.log('[child] - message read from thread : "', content, '"');
      });

      thread.send("Hello I'm Child =)"); // send a message to the main thread
      // END
    });
    
    child.response(content => { // retrieve only the communication from the child
      console.log('[root] - message read from child : "', content, '"');
    });
    // ---

    nthread.getByGuuid(child.getGuuid()).send("Hey Child, how are you?"); // send a message to the child thread

    // nthread.close(); // close all threads
  });

More examples

📝 Usage listen(string: port = random port, object: options = undefined)

Options

NameTypeDescription
tmpFolder = '%default_tmp_folder%/nthread_generated'StringTemporary folder used to save js code
secure = falseBooleanUse protocol http or https
debug = falseBooleanEnable debug
port = random portNumberServer port
socket = undefinedObjectSocket.IO API
server = undefinedObjecthttp API https API

Functions from Nthread

Nthread is the return of listen function, to create new threads and manage them.

NameReturnDescription
getPublicUri()StringRetrieve public uri
getLocalUri()StringRetrieve local uri
getByGuuid()Object: CThreadRetrieve CThread by Guuid
send(any: content)UndefinedSend a message to all threads
response(function: callback)Promise: anyCall back for each response from any threads
createFromIO(object: Socket.Io Client)Promise: CThreadCreate a new thread from Socket.Io Client
create(code: string)Promise: CThreadCreate a new thread from a lambda
load(string: file_path)Promise: CThreadCreate a new thread from a file with a module.exports = () => { ... } inside
close()UndefinedClose every connection of each thread

Functions from CThread

CThread is the return after the promise of create/createFromIO/load function, to manage the current thread and communicate with the thread from the main thread.

NameReturnDescription
getPid()NumberRetrieve pid of the thread
getGuuid()StringRetrieve Guuid of CThread
send(any: content)UndefinedSend a message to the current thread
response(function: callback)Promise: anyCall back response from the current thread
on(string: instruction)UndefinedRetrieve evenements sent by thread process ('*', 'stdout', 'stderr', 'disconnect')
close()UndefinedClose the connection of the current thread

Functions from the parameter thread in create/createFromIO/load callback

thread is the first parameter of the create/createFromIO/load callback, the code inside is running in another process, the thread.

NameReturnDescription
getPid()NumberRetrieve pid of the thread
getGuuid()StringRetrieve Guuid of CThread
log()UndefinedLog a message directly in the main thread
send(any: content)UndefinedSend a message to the main thread
response(function: callback)Promise: anyCall back response from the thread
on(string: instruction)UndefinedRetrieve evenements sent by thread process (disconnect)
close()UndefinedClose the connection of the thread

📝 Usage connect(string: uri, object: options = undefined)

Options

NameTypeDescription
guuid = nullStringSet a nthread-js guuid
debug = falseBooleanEnable debug
socket = undefinedObjectSocket.IO API

CThread is the return after the promise of connect function, to manage the thread and communicate with the main thread.

NameReturnDescription
getPid()NumberRetrieve pid of the thread
getGuuid()StringRetrieve Guuid of CThread
log()UndefinedLog a message directly in the main thread
send(any: content)UndefinedSend a message to the main thread
response(function: callback)Promise: anyCall back response from the thread
on(string: instruction)UndefinedRetrieve evenements sent by thread process (disconnect)
close()UndefinedClose the connection of the thread

Keywords

FAQs

Package last updated on 01 Oct 2019

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