@@ -0,0 +1,141 @@
## Getting started
A basic installation of [ Node.js] ( https://nodejs.org/ ) , [ Express] ( https://expressjs.com/ ) , and [ Socket.io] ( https://socket.io/ ) served over ` https:// ` using a [ Letsencrypt SSL] ( https://letsencrypt.org/ ) certificate.
### Step 1 - Build DO Droplet (or server of your choice)
- Spin up DO droplet
- Set up DNS
- Access the server via SSH
### Step 2 - Install dependencies
- Update server: ` apt-get update `
- Install Nodejs: ` apt-get install nodejs `
- Install NPM: ` apt-get install npm `
- Install Express: ` npm install express `
### Step 3 - Create node app
- Build app: ` npm init `
- Give the app a name: ** Socket**
- Complete other fields as desired or leave blank
- Confirm details: ` yes `
### Step 4 - Install socket.io
- Install socket.io: ` npm install socket.io --save `
### Step 5 - Install socket.io Javascript client library
- Visit socket.io [ Javascript client library] ( https://github.com/socketio/socket.io-client )
- Enter the ** dist** folder
- Select the ** socket.io.js** file
- Copy the files URL: [ https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.js ] ( https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.js )
- Clone the file to the server using:
``` wget https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.js ```
### Step 6 - Create index.js
- Create a file called index.js: ` nano index.js `
- Add the following to the new ` index.js ` :
``` js
var app = require (' express' )()
var server = require (' http' ).Server (app)
var io = require (' socket.io' )(server)
var count = 0
app .get (' /' , function (request , response ) {
response .sendFile (' /root/index.html' )
})
io .on (' connection' , function (data ) {
count++
data .send (count + " active sockets" )
})
server .listen (80 )
```
### Step 7 - Create index.html
- Create a file called index.html: ` nano index.html `
- Add the following to the new ` index.html ` :
``` html
<script src =" /socket.io/socket.io.js" ></script >
<script >
var socket = io ()
socket .on (' message' , function (data ) {
document .write (data)
})
</script >
```
### Step 7 - Run the node server
- Run node: ` nodejs index.js `
The site can now be viewed over ` http:// ` .
## Install Letsencrypt SSL
Install Certbot:
``` sudo apt install certbot ```
Now run CertBot and follow instructions, selecting ** 1** as the option to continue:
``` sudo certbot certonly ```
## Set up index.js
Update ` index.js ` to include the following, replacing EXAMPLE.COM with the live domain:
``` js
var app = require (' express' )()
// var server = require('http').Server(app)
var https = require (' https' );
var fs = require ( ' fs' );
var server = https .createServer ({
key: fs .readFileSync (' /etc/letsencrypt/live/EXAMPLE.COM/privkey.pem' ),
cert: fs .readFileSync (' /etc/letsencrypt/live/EXAMPLE.COM/cert.pem' ),
ca: fs .readFileSync (' /etc/letsencrypt/live/EXAMPLE.COM/chain.pem' ),
requestCert: false ,
rejectUnauthorized: false },app);
var io = require (' socket.io' )(server)
var count = 0
app .get (' /' , function (request , response ) {
response .sendFile (' /root/index.html' )
})
io .on (' connection' , function (data ) {
count++
data .send (count + " active sockets" )
})
server .listen (443 );
```
Run node ` nodejs index.js ` and view site over ` https:// ` .