MASK v2.0 – Local server(*2) network!

Screen Shot 2015-09-18 at 8.40.12 PM

Quick notes on how I made local Peer.js server, and also closed network!

When making my thesis, I used cloud hosted PeerServer with API key, so the project was constrained by internet access. In order to make the project more robust and stable, I decided to create PeerServer on my own laptop, and let devices (mobile phones) access the laptop (server) through Wi-Fi.

Here are the quick notes on how I did those (OMG such a googling / debugging hell!):

 

Create PeerServer accompanying another WebSocket system

Peer.js documented well about how to create your own PeerServer, even with examples of combining with existing express app, which is perfect for me. But because I have my own Node server as well, I struggled for a while to realize that, I should create two servers for each websocket with different ports!!! Finding this out through non-stop console.log and try & error!

Server codes

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 7000;

// peer_server
var ExpressPeerServer = require('peer').ExpressPeerServer;
var peerExpress = require('express');
var peerApp = peerExpress();
var peerServer = require('http').createServer(peerApp);
var options = { debug: true }
var peerPort = 9000;

app.get('*', function(req, res){
    res.sendFile(__dirname + req.url);
});
peerApp.use('/pp', ExpressPeerServer(peerServer, options));

server.listen(port);
peerServer.listen(peerPort);

 

Client side sudocode

var peerConnect = function() {			
	/* WebRTC - Peer.js */
	peer = new Peer( ID(), { secure: false, host: 'YOUR_IP', port: 5000, path:'/pp', debug:true} );

	// Get an ID from the PeerJS server		
	peer.on('open', function(id) {
		console.log('My peer ID is: ' + id);
		peer_id = id;

		// Now we can connect to my socket server						
		connectSocket();
	});

	peer.on('call', function(incoming_call) {
		console.log("Got a call!");
		incoming_call.answer(my_stream); // Answer the call with an A/V stream.
		// do what you want~~~
	});
};

 

MacbookPro as server, accessing by devices through Wi-Fi with router

  1. Connect Router with MacbookPro
  2. Click Wi-Fi symbol to expand the list –> Create Network…Screen Shot 2015-09-19 at 12.43.25 PM
  3. Setup name and passwordScreen Shot 2015-09-19 at 12.44.53 PM
  4. System Preferences… –> SharingScreen Shot 2015-09-19 at 12.46.06 PM
    • Setup computer name
    • Click Internet Sharing
    • Share your connection from: Ethernet
    • To computer using: Wi-Fi
    • Click Wi-Fi Options –> setup Wi-Fi name and passwordScreen Shot 2015-09-19 at 12.48.59 PM
    • Click on box next to Internet Sharing to startScreen Shot 2015-09-19 at 12.50.45 PM
  5. @ your device’s Wi-Fi list, choose the Wi-Fi shared by MacbookPro and enter password
  6. Voila! Now your device and MacbookPro have same IP address, thus the device and go to the page that MacbookPro serves with <IP_address>:7000/index.html.