This repository demonstrates how to use Node for Max to create a simple websockets application with express
, socket.io
, and a public URL via ngrok
.
Be sure to also check out the examples in the official Cycling ’74 n4m-examples
repository!
-
Download this repository and open
index.maxpat
.# using the command line: git clone https://github.com/delucis/n4m-socket-demo.git cd n4m-socket-demo open index.maxpat
-
In Max, click the
script npm install
message button to install the dependencies for this repository. (You could also do this from the command line by runningnpm install
in the repository directory). -
Press
script start
to launch the web server and create a tunnel to ngrok. -
Open your browser to http://0.0.0.0:3000 or the ngrok URL displayed on-screen
-
Try typing and sending messages using the browser interface. You should be able to receive them inside Max. Hooray! 🎉
ngrok
allows you to sign up and then authenticate your session using a secret token. It would be a bad idea to commit that to GitHub, because then everyone would see your secrets 😱
Instead, we can have our index.js
code look for an “environment variable” (available as a property of the process.env
global variable in Node) and use that if available.
By default this example will not authenticate with ngrok
and is therefore subject to certain restrictions. To provide a secret token create a file called .env
in the root directory of this repository and add the following line:
NGROK_TOKEN=MyVerySecretTokenFromNgrok
This example uses the dotenv
Node package to load the variables declared in your .env
file into the environment variables. As mentioned above, do not upload your secrets to GitHub.
To get a token you should sign up for an ngrok account. Once you have an account, go get your authtoken.