What is Node?
*This tutorial was designed to work with a Raspberry Pi running Jessie but should work on other platforms.
Node.js is a very efficient and popular way to manage and create web applications. This tutorial walks you through how to set up a node server. A sever is a computer that can take and process requests from clients (a client is a computer connected to a server).
Servers can do many things including:
· Pass information between two or more clients.
· Store large amounts of information that clients can access.
· Give clients control over the server’s hardware. (The GPIO on the Pi)
For more information check out www.nodejs.org. There are huge communities of developers that use Node.js to do amazing things. Google it!
I also found this blog post very helpful: danielnill.com/nodejs-tutorial-with-socketio/ You should read it.
Before you go on…
Make sure Node.js and socket.io are installed.
To check if Node is installed open a terminal window and type in node –v. This should display the current version or an error, in which case you will need to install node first.
If you need to install node try these instructions: http://joshondesign.com/2013/10/23/noderpi
1. Open a terminal window
2. Run node in interactive mode by just typing node
3. Type require(‘socket.io’)
If you get an error that says “Cannot find module” it is not installed.
This will install socket.io for you npm install socket.io
Setting up the Server
1. It will listen for potential clients on a specified port.
2. Our clients will be requesting an html page, the server will send the client the right one.
3. The server will send and receive messages from the client using the socket connection.
4. The server will handle any errors that might come up.
Step 1: Setting up Folders and Files
- Make a new folder in the Public directory called simpleServer. Save your files in here.
- Make one empty file called simpleServer.js
- Make one empty file called socket.html
Step 2: The server
This code will get a basic server up and running for you:
Paste it into simpleServer.js and save.
Step 3: The Client
Socket.html is the web page that will be given to the client computer. It will establish the socket connection and handle all user interactions. (Buttons, messages, various user input etc)
Save this in the same folder as simpleServer.js, call it socket.html
Test your system by trying to access it from am iPad or phone on the guest network. Make sure you get messages working both ways. If you are having trouble….
1. Make sure you have the right IP address and Socket.
2. Make sure both computers are on the same network.
3. Use console.log() in strategic places to help you see what is happening.
4. Test your html page on its own to make sure it works properly.