npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nthread-js

v2.0.0

Published

Create easily children thread (process) in NodeJs. The purpose is to delegate some part of your code in another process.

Downloads

4

Readme

Nthread JS

Create easily children thread (process) in NodeJs. The purpose is to delegate some part of your code in another processus.

⚠ 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 and... cash.

☁️ Installation

$ npm install nthread-js

⚙️ Examples Parent + create callback child process

const { Parent } = require('../lib/index');
const parent = new Parent({ debug: false });

parent.listen(3000).then(async nthread => {

  console.log("[root] - Server connected");
  console.log("[root] - public uri", nthread.getPublicUri());
  console.log("[root] - local uri", nthread.getLocalUri());

  nthread.on('*', content => console.log(content));

  const child = await nthread.create(thread => {
    thread.log('[child] - is now connected with PID: ' + thread.getPid());
    
    thread.response(content => {
      console.log("[child] - response", content);
      content === "Hello" ? thread.emit('Hi back !') : thread.emit('Fine and you?');
    });
  });

  child.emit('Hello');

  // child.response(content => {
  nthread.response(({ client }, content) => {
    console.log("[root] - response", content)
    content === "Hi back !" ? child.emit('How are you ?') : null;
  });
});

⚙️ Examples Child trying to connect to the parent process

const { Child } = require('../lib/index');
const child = new Child({ debug: true });

child.connect("http://127.0.0.1:3000").then(async thread => {
  thread.log('[child] - is now connected with PID: ' + thread.getPid());
  
  thread.emit('Hi back !');

  thread.response(content => {
    console.log("[child] - response", content);
    content === "How are you ?" ? thread.emit('Fine and you?') : null;
  });
});

More examples

📝 Parent usage

const parent = new Parent({
  // options
});

Options parameter

| Name | Type | Description
| ------------------------------------------------------ | -------- | ------------ | tmpFolder = '%default_tmp_folder%/nthread_generated' | String | Temporary folder used to save js code | secure = false | Boolean | Use protocol http or https | debug = false | Boolean | Enable debug | socket = undefined | Object | Socket.IO API | server = undefined | Object | http API https API

async listen(port: number = random, callback: function = undefined) : Promise<Thread>

Establishing a socket server connection to communicate between the parent and the children process.

⚙️ Thread usage

After the connection is established, listen return a Thread instance.

| Name | Return | Description
| ----------------------------------------| ----------------------| ------------ | async create(thread_code: function) | Promise | Create a new Thread from a thread_code | async load(path_file: string) | Promise | Create a new Thread from an import code | async close() | Undefined | Close every ChildThreads connection | emit(content: any) | Undefined | Emit content to all the children | async response(function: callback) | Promise<...any> | Callback will be called for each response doesn't matter the child | getAllChildrenThread() | Array | Retrieve all the children | getChildThreadByGuuid(guuid: string) | ChildThread | Retrieve a specific ChildThread by guuid | getPublicUri() | String | Retrieve public uri | getLocalUri() | String | Retrieve local uri | async on(event: string, callback: function) | Promise<...any> | Listen an event

on evenements

event for child_process : stdout, stderr, exit and close
event for client : connected, disconnected, response and log

| Name | Return | Description
| ----------------------------------------| ----------------------| ------------ | * | { event: string, guuid: string, content: any } : object | child_process | { event: string, guuid: string, content: any } : object | child_process{event} | { guuid: string, content: any } : object | {guuid}child_process | { event: string, content: any } : object | {guuid}child_process{event} | content: any | client | { event: string, guuid: string, content: any } : object | client{event} | { guuid: string, content: any } : object | {guuid}client | { event: string, content: any } : object | {guuid}client{event} | content: any

⚙️ ChildThread usage

After the thread creation, create or load return a ChildThread instance.
ChildThread is not the child process !!! But it give you the possibility to communicate with it.

| Name | Return | Description
| ----------------------------------------| ------------------| ------------ | emit(content: any) | Undefined | Emit content | async response(callback: function) | Promise<...any> | Callback as soon the childProcess send a message | async close() | Undefined | Close the connection | getPid() | Number | Retrieve pid | getGuuid() | String | Retrieve Guuid | async on(event: string, callback: function) | Promise<...any> | Listen an event

📝 Child usage AKA thread_code or path_file

const child = new Child({
  // options
});

Options

| Name | Type | Description
| ------------------------------------------------------ | -------- | ------------ | debug = false | Boolean | Enable debug | socket = undefined | Object | Socket.IO API

async connect(url: string, guuid: string = undefined) : Promise<Thread>

Establishing a socket connection to the Parent thread.

⚙️ Thread usage

| Name | Return | Description
| ----------------------------------------| ------------------| ------------ | log(content: any) | Undefined | Log a message directly in the parent thread | emit(content: any) | Undefined | Emit content to the parent thread | async response(callback: function) | Promise<...any> | Callback as soon the childThread send a message | async close() | Undefined | Close the connection | getPid() | Number | Retrieve pid | getGuuid() | String | Retrieve Guuid | async on(event: string, callback: function) | Promise<...any> | Listen an event

⚠️ ChildThread IS NOT ChildProcess

After created ChildThread, a ChildProcess is created.
The ChildProcess is another processus within your thread_code or your path_file is executed.
ChildThread is an instance from the Parent to communicate with the ChildProcess.
With emit and response.

👥 Contributing

Please help us to improve the project by contributing :)

❓️ Testing

$ npm install
$ npm test