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 real thread in Node Js

  • 0.0.1
  • Source
  • npm
  • Socket score

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

Nthread JS

Create real thread in Node Js

Install

npm install --save nthread-js

Usage

const nthread = require('nthread-js')( /* options */ ); // Initialize with option

nthread.ready(async (mthread) => // Waiting of the starting process [Parent in Main Thread]
{
	// create a new thread from lambda [Child in Main Thread]
	let child = await mthread.create(async (thread) => { 
		let params = thread.params;
		thread.send(`Now I'm sending child params : ${JSON.stringify(params)}`);
		let pres = await thread.response();
		console.log('CHILD', pres);
	}, 'testOne', 'testTwo');

	// Listen stdout of child
		child.stdout((data) => {
		console.log('child stdout', data);
	});
	
	// Process main thread
	await child.ready();
	let cres = await child.response();
	console.log('ROOT', cres);
	child.send(`Now I'm sending a message to my child`);

	setTimeout(() => { console.log('Exit system'),mthread.exit() }, 3000);

}).catch(err => console.log(err));

More examples

Options from Main Thread

NameTypeDescription
tmpFolder = 'generated'StringName of folder who's all temporary files is saved
protocol = 'http://'StringProtocol
enableSocket = trueBooleanIf enable socket is false so only connect function is available in parent
port = randomNumberPort

Functions from Parent in Main Thread

NameTypeDescription
ipStringGet public ip
localIpStringGet local ip
portStringGet port
addressStringGet local uri
localAddressStringGet local uro
connect(string: uri, any: param1, ...) : PromiseFunction: PromiseMake a connection to an external thread/app
create(function: callback, any: param1, ...) : PromiseFunction: PromiseCreate a new thread with a callback
connection(function: callback) : PromiseFunction: PromiseWait a new external connection
exit(number: code = 0)FunctionClose all thread and all connection

Functions from Children in Main Thread

NameTypeDescription
guidStringGet guid of child
paramsStringRecover your initial parameters set from child thread
send(any: data)FunctionSend data to the main thread
response(function: callback) : PromiseFunction: PromiseWait response from the main thread
ready(function: callback) : PromiseFunction: PromiseIt's an event called after child has finished to initialised
stdout(function: callback) : PromiseFunction: PromiseIt's an event called if the thread write in process (ex: console.log)
stderr(function: callback) : PromiseFunction: PromiseIt's an event called if the thread have an error in process (ex: console.error)
exit(function: callback) : PromiseFunction: PromiseIt's an event called if the thread process has exit
disconnect()FunctionClose child connection

Functions in Children Thread

NameTypeDescription
paramsArrayRecover your initial parameters sent from main thread
send(any: data)FunctionSend data to the main thread
response(function: callback) : PromiseFunction: PromiseWait response from the main thread

Keywords

FAQs

Package last updated on 14 Jun 2018

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