Node for Max - Script Lifecycle

The node.script lifecycle

Lifecycle events

When you start and stop your Node scripts, the node.script object that contains the script will send messages out of its rightmost outlet to let you know about the status of the running script. These are the messages that the node.debug object receives and parses in order to configure its display.

Lifecycle events

There are seven states of your program that we associate with starting and stopping a script in a node.script object:

  1. Start
  2. Loadstart
  3. Loadend
  4. Stop
  5. Terminated
  6. Restarting
  7. Restarted

Each of these states has its own specific collection of messages associated with it. These messages have a specific format.

When you query the status of your script each of these messages will be output, followed by the symbol success or error, depending on whether or not that part of the lifecycle has completed successfully or not.

Following the success or error symbol, there will be a reference to a dictionary (such as u23732230) which contains further information about the success of failure of your script.

Here’s an example of a typical message:

start success dictionary u23982374

In the case of an error, you’ll also see a message describing what went wrong. Here’s what those messages will generally indicate, depending on the lifecycle event:

start node.script attempted to start, either in response to a script start message or because the object was created with @autostart 1.
Note that when receiveing a start success status the user defined script has not been loaded yet. It just indicates that the process is running and all underlying inter-process communication has been established by node.script.
loadstart Indicator status to inform that node.script is about to load the user defined.
loadend Indicator status to inform that node.script has finished loading the user defined.
You can use this message as an indicator that your entry point script has been loaded and is ready to process any incoming messages.
stop node.script received a script stop message and is attempting to stop.
terminated the Node process managed by node.script terminated. This could be due to an error, or because node.script received a stop message, or because @watch 1 was enabled and the source file changed.
restarting node.script is attempting to restart.
restarted node.script restarted and the script is now running.

Automatic Restarting

If your Node script crashes, node.script will attempt to restart automatically. You will see a message in the Max window as well ('Node child process quit, will restart').

If the Node script crashes again, immediately after starting, it will attempt to restart up to 5 times. After that, node.script won’t run again until it receives a script start message.

Status dictionaries

Following the success or error symbol, there will be a reference to a dictionary which contains further information about the success of failure of your script. The contents of those dictionaries vary according to the type of lifecycle. The dictionary contents are described below:

Start

id A unique ID for this Node process
watch Indicates whether the node process was started with @watch 1 (1) or not (0).
pid The process ID of the child Node process on the host machine.
cwd The “current working directory” of the Node process.
filename The name of the source file for the Node process.
socketDescription A description of the interprocess communication socket through which Max and Node communicate.
debugEnabled Indicates whether debug mode is enabled (1) or not (0).

Loadstart

id The ID of the stopped Node process, which will match an ID from the start dictionary.

Loadend

id The ID of the stopped Node process, which will match an ID from the start dictionary.

Stop

id The ID of the stopped Node process, which will match an ID from the start dictionary.

Terminated - Success

id The ID of the stopped Node process, which will match an ID from the start dictionary.
willRestart Indicates whether the node.script object will automatically restart the Node process (1) or not (0).

Terminated - Error

message The error message returned from Node, describing what happened.
details Error details from Node that describe why it happened.
stack A string detailing the call stack at the time the application encountered an error.
code The error code associated with the error.

Restarting

id The unique ID of the started Node process.
reason This entry will either be error or file_change depending on the reason for the restart.
restartCount The number of times the process has restarted.

Restarted

id The unique ID of the started Node process. This ID will match with the ID from “restarting”.
pid The process ID of the child Node process on the host machine.
reason This entry will either be error or file_change depending on the reason for the restart.
restartCount The number of times the process has restarted.

See Also

Name Description
Node for Max Documentation - Table of Contents Node for Max Documentation - Table of Contents
node.script