<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<?xml-stylesheet href="./_c74_ref.xsl" type="text/xsl"?>

<!--Data marked TEXT_HERE, INLET_TYPE, OUTLET_TYPE, ARG_NAME and ARG_TYPE need to be filled in-->
<c74object name="node.script" module="" category="">
	<digest>
		Control a local Node.js process from Max
	</digest>
	<description>
		Control a local Node.js process from Max. Messages preceded by <m>script</m>
		 control the lifecycle of the Node script (start to start, stop to stop). Other messages will be sent
		 to the process directly.

	</description>
	<discussion>
		<p>
			Node is a JavaScript framework for writing applications. To learn more about Node,
			visit their <a href="https://nodejs.org/">website</a>.
		</p>
		<p>
			Some Max patchers will want to do things that are difficult to do from Max. For example, it might be desirable to
			 pull some data from a web address, then parse and analyze that data to generate synthesis parameters. Or, one might
			 want to watch a local folder for files, triggering some action inside Max when the contents of that folder change.
			 For these kinds of long-running tasks, which may require some kind of complex, persistent state, Node can be a very
			 useful tool.
		</p>
		<p>
			Another reason to use Node is to take advantage of the wealth of open source modules registered with the Node Package
			 Manager (NPM). These packages contain libraries of JavaScript code for doing everything from parsing dates, to training
			 neural nets, to running a web server, to writing chord progressions and much more. Node for Max helps make these resources
			 available to Max programmers.
		</p>
		<p>
			To communicate with Max, the Node script uses the "max-api" Node module. This set of functions simplifies the process
			 of sending data between the Node application and Max. This API can attach handlers to Max messages; parse Max dictionaries
			 into JSON objects; and read, update and set named dictionaries within Max. For examples on how to use this API, see the
			 code examples included with the Node for Max package, as well as the API documentation at
			<a href="https://docs.cycling74.com/maxnode.docs/max-node-api/">https://docs.cycling74.com/maxnode.docs/max-node-api/</a>
		</p>
	</discussion>
	<discussion title="Differences from the js object">
		<p>
			The <o>node.script</o> object refers to a JavaScript source file, so it has some overlap with the <o>js</o> object. However, there
			 are some important differences. Unlike <o>js</o>, <o>node.script</o> runs in a separate process. This has some benefits when
			 it comes to parallelization, since Max and the Node script can run on separate cores. The Node script is a full application, which
			 means that once it's started it has its own run loop, and has total freedom with respect to what code it can run when. It responds to
			 events just like any other application, and can even have its own Graphical User Interface (GUI). Messages sent to the Node script will
			 be executed asynchronously, since Node and Max are running in separate processes. Finally, scripts running with <o>node.script</o> have
			 access to the full Node library for network communication, file I/O and more.
		</p>
	</discussion>
	<!--METADATA-->
	<metadatalist>
		<metadata name="author">
			Cycling '74
		</metadata>
		<metadata name="tag">
			node
		</metadata>
	</metadatalist>
	<!--INLETS-->
	<inletlist>
		<inlet id="0" type="INLET_TYPE">
			<digest>
				TEXT_HERE
			</digest>
			<description>
				TEXT_HERE
			</description>
		</inlet>
	</inletlist>
	<!--OUTLETS-->
	<outletlist>
		<outlet id="0" type="OUTLET_TYPE">
			<digest>
				Anything sent using the outlet function in Node
			</digest>
		</outlet>
		<outlet id="1" type="OUTLET_TYPE">
			<digest>
				Dump out: stdout and stderr, as well as status updates
			</digest>
		</outlet>
	</outletlist>
	<!--ARGUMENTS-->
	<objarglist>
		<objarg id="0" name="script" type="symbol" optional="0">
			<digest>
				Node script
			</digest>
			<description>
				Entry point for your Node application. Send <o>node.script</o> the <m>start</m> message to start running
				 this script.
			</description>
		</objarg>
	</objarglist>
	<!--MESSAGES-->
	<methodlist>
		<method name="bang">
			<arglist />
			<digest>
				Send a bang message to the running Node script
			</digest>
		</method>
		<method name="anything">
			<arglist>
				<arg name="anything" type="list" optional="0" />
			</arglist>
			<digest>
				Send a message of any length to the running Node script. Can also be used to send dictionaries, which will automatically
				 be converted to JSON.
			</digest>
		</method>
		<method name="api">
			<arglist />
			<digest>
				Launch the JS documentation
			</digest>
		</method>
		<method name="(mouse)">
			<digest>
				Double-click to reveal the Node script
			</digest>
		</method>
		<method name="reveal">
			<arglist />
			<digest>
				Reveal the Node script in Finder or Explorer
			</digest>
		</method>
		<method name="script">
			<arglist>
				<arg name="arguments" type="list" optional="0" />
			</arglist>
			<digest>
				Start, stop and query the Node process
			</digest>
			<description>
				<p>
					Messages beginning with the symbol <m>script</m> are used to start, stop and query the Node process. The symbol
					 following <m>script</m> determines how the message will be handled. Subsequent arguments are passed to that handler, if applicable.
				</p>
				<p>
					npm - Interface with the Node Package Manager (npm). If the node script is in a node package, then this message can be used
					 to (for example) install module dependencies listed in package.json into node_modules. The npm message assumes that
					 package.json is in the same folder as the script specified as the first argument to node.script. Symbols following npm will be
					 sent as arguments to npm (for example <m>script npm install</m> will run `npm install`).
				</p>
				<p>
					processStatus - Get a dictionary describing the status of the running Node process (if any)
				</p>
				<p>
					reboot - Rarely used. Reboot the Node process manager if it has crashed.
				</p>
				<p>
					running - Returns 1 if there is a running node process, and 0 otherwise
				</p>
				<p>
					status - Returns a dictionary with information about the installed version of Node
				</p>
				<p>
					start - Start running the Node script with the given (optional) arguments. Arguments following <m>start</m> will be
					 available as command line arguments to the Node script.
				</p>
				<p>
					stop - Stop running the Node script
				</p>
			</description>
		</method>
		<method name="stdin">
			<arglist>
				<arg name="stream" type="list" optional="0" />
			</arglist>
			<digest>
				Send a character stream directly to stdin
			</digest>
			<description>
				Send a character stream directly to stdin. Anything sent to <o>node.script</o> after <m>stdin</m> will be sent
				 directly to standard input of the running node script. Note that Max will add a newline character to the end
				 of whatever message is sent.
			</description>
		</method>
	</methodlist>
	<!--ATTRIBUTES-->
	<attributelist>
		<attribute name="args" get="1" set="1" type="atom" size="16">
			<digest>
				Arguments to be passed to the Node script on launch
			</digest>
			<description>
				Arguments to be passed to the Node script on launch. Note that this attribute is only meaningful in conjunction
				 with the <at>autostart</at> attribute. Otherwise, node.script will use the arguments supplied with the
				 <m>start</m> message.
			</description>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Arguments to be passed to the Node script on launch" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
			</attributelist>
		</attribute>
		<attribute name="autostart" get="1" set="1" type="int" size="1">
			<digest>
				Start the Node script automatically, without <m>script start</m>
			</digest>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Start automatically" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="onoff" />
			</attributelist>
		</attribute>
		<attribute name="defer" get="1" set="1" type="int" size="1">
			<digest>
				Whether to defer messages from Node to the low priority queue
			</digest>
			<description>
				When false (the default), messages from Node will be processed on the scheduler thread,
				at the same priority as other Max messages. Activating defer will move processing to the back of
				the low priority queue, same as the defer attribute on <o>metro</o>
			</description>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Defer" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="onoff" />
			</attributelist>
		</attribute>
		<attribute name="log_path" get="1" set="1" type="symbol" size="1">
			<digest>
				Debug only: specify a path to which a debug log will be written
			</digest>
			<description>
				Debug only: specify a path to which a debug log will be written, for example ~/Documents/debug.txt
			</description>
		</attribute>
		<attribute name="node_bin_path" get="1" set="1" type="symbol" size="1">
			<digest>
				Override the built in Node executable
			</digest>
			<description>
				Override the built in Node executable. Optional. Only use this attribute if you'd like to use a different
				 Node executable from the one included with Max for Node (possibly you want to use an older version for
				 compatibility reasons).
			</description>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Node Executable" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="text" />
			</attributelist>
		</attribute>
		<attribute name="npm_bin_path" get="1" set="1" type="symbol" size="1">
			<digest>
				Override the built in NPM executable
			</digest>
			<description>
				Override the built in NPM executable. Optional. Only use this attribute if you'd like to use a different NPM
				 executable from the one included with Max for Node (possibly you want to use an older version for
				 compatibility reasons).
			</description>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="NPM Executable" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="text" />
			</attributelist>
		</attribute>
		<attribute name="pm_path" get="1" set="1" type="symbol" size="1">
			<digest>
				Debug only: specify the path to the process manager script
			</digest>
		</attribute>
		<attribute name="restart" get="1" set="1" type="int" size="1">
			<digest>
				Restart on crash
			</digest>
			<description>
				When <at>restart</at> is enabled (default) <o>node.script</o> will restart automatically after a node code
				crash inside a handler callback. If <at>restart</at> is disabled, automatic restarts are suppressed.
			</description>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="default" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Restart on crash" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="onoff" />
			</attributelist>
		</attribute>
		<attribute name="running" get="1" set="0" type="int" size="1">
			<digest>
				Read only: 1 if there is a running Node process, 0 otherwise
			</digest>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="0" type="symbol" size="1" value="Running" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="0" type="symbol" size="1" value="onoff" />
			</attributelist>
		</attribute>
		<attribute name="watch" get="1" set="1" type="int" size="1">
			<digest>
				True if <o>node.script</o> should relaunch the Node script automatically when the source file changes
			</digest>
			<attributelist>
				<attribute name="basic" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="label" get="1" set="1" type="symbol" size="1" value="Watch Source" />
				<attribute name="save" get="1" set="1" type="int" size="1" value="1" />
				<attribute name="style" get="1" set="1" type="symbol" size="1" value="onoff" />
			</attributelist>
		</attribute>
	</attributelist>
	<!--EXAMPLE-->
	<examplelist>
		<example caption="" img="" />
	</examplelist>
	<!--SEEALSO-->
	<seealsolist>
	</seealsolist>
</c74object>
