How to PeerJS over the Internet? - javascript

in the local LAN everything is working very well, but over the internet it doesn't work.
i read a lot about WebRTC Signaling.
i use the following node.js peerjs server
whats wrong with my config?
var fs = require('fs');
var PeerServer = require('peer').PeerServer;
var server = PeerServer({
port: 3001,
debug: true,
path: '/peerjs',
ssl: {
key: fs.readFileSync('privkey.pem', 'utf8'),
cert: fs.readFileSync('fullchain.pem', 'utf8')
},
config: {'iceServers': [
{ url: 'stun:stun.l.google.com:19302' },
]}
});

First of all, you need to use a PeerServer that is not on your local network (=accessible to the internet). There is one provided by peer.js, which is used by default when no PeerServer URL is specified by the client.
To establish a connection, a library like socket.io can be very useful. This video gives a good explanation: https://www.youtube.com/watch?v=DvlyzDZDEq4

Related

Creating Node.JS HTTPS server in Cloud9 IDE

Is it somehow possible to create a Node.js https server in cloud9 IDE?
Below is my example of simple https server setup in Node.js.
var https = require('https');
var fs = require('fs');
var app = require('./app');
// SSL Configuration
var ca_names = ['CERT-NAME_1', 'CERT-NAME_2', 'CERT-NAME_3'];
var options = {
key: fs.readFileSync('./folder/server.key'),
cert: fs.readFileSync('./folder/server.crt'),
ca: ca_names.map(function(n) {
return fs.readFileSync('./eid/ca/' + n + '.crt');
}),
//crl: ca_names.map(function(n) { return fs.readFileSync('/eid/ca/' + n + '.crl'); }),
requestCert: false,
rejectUnauthorized: false
};
var server = https.createServer(options, app);
server.listen(process.env.PORT || 8081, process.env.IP || "0.0.0.0");
console.log('server listening on port: ' + process.env.PORT);
when I try to connect to the server then I am getting following error:
"ECONNRESET: Request could not be proxied!"
I think the problem is you are trying to listen to both HTTP and HTTPS.
c9 works as a proxy so you only need to listen on HTTP even though you are trying to use HTTPS. Try not listening to HTTPS and it should work. (more info on this)
But, if you really need HTTPS, in that case you can use a proxy like Nginx to internally proxy requests over HTTPS.(more info on this)enter link description here

Serve two https hostnames from single node process & port

Is there a way to serve two https sites on the same port from node alone (i.e. without something like nginx)?
I'm using the https module, for which SSL options (e.g. key/cert/ca) are passed into createServer. I tried creating two https servers, each listening to a different hostname, and each with SSL options specific to that hostname:
require('https').createServer(sslOptsForFoo, expressApp).listen(443, 'foo.com');
require('https').createServer(sslOptsForBar, expressApp).listen(443, 'bar.com');
But this throws an error:
Error: listen EADDRINUSE
Another idea is to create a single server rather than two, and to use different SSL options depending on the request's hostname, but I'm not sure if this is possible.
You can only do this with SNI so you may want to check for SNI support with various browsers first.
Here's one way to do SNI in node:
var https = require('https'),
fs = require('fs');
// default for non-matching requests
var serverOptions = {
key: fs.readFileSync('default.pem'),
cert: fs.readFileSync('default.crt')
};
var SNIContexts = {
'foo.com': {
key: fs.readFileSync('foo.com.pem'),
cert: fs.readFileSync('foo.com.crt')
},
'bar.com': {
key: fs.readFileSync('bar.com.pem'),
cert: fs.readFileSync('bar.com.crt')
}
};
var server = https.createServer(serverOptions, function(req, res) {
res.end('Hello world via ' + req.socket.cleartext.servername);
});
server.addContext('foo.com', SNIContexts['foo.com']);
server.addContext('bar.com', SNIContexts['bar.com']);
server.listen(443, function() {
console.log('Listening on 443');
});

Socket.IO application not starting, no output

I have the following simple socket.io app :
var fs = require('fs');
var db = require("./libs/db.js");
var sslOptions = {
key: fs.readFileSync('/var/ssl/server.key'),
cert: fs.readFileSync('/var/ssl/server.crt'),
ca: fs.readFileSync('/var/ssl/ca.crt'),
requestCert: true,
rejectUnauthorized: false
};
var io = require('socket.io').listen(4000,sslOptions);
It runs, displaying : info: socket.io started on launch. The page example.com:4000 outputs : Welcome to socket.io, all good.
Now, in another directory, I have :
var fs = require('fs');
var sslOptions = {
key: fs.readFileSync('/var/ssl/server.key'),
cert: fs.readFileSync('/var/ssl/server.crt'),
ca: fs.readFileSync('/var/ssl/ca.crt'),
requestCert: true,
rejectUnauthorized: false
};
var io = require('socket.io').listen(2000,sslOptions);
That code (exactly the same??) doesn't output anything on launch...
socket.io versions are identical (1.1.0), both directories are chmoded the same.
What could cause such behaviour? How can I debug?
Edit :
When I cp the not working app in the same dir than the working app, it runs... Again, permissions are the same.
I copied the node_modules/socket.io of the working dir into the non-working one. It now works.
Still haven't got a clue what's happening. Both versions were installed using npm install, and npm socket.io version gave me the same "1.1.0".
I'll leave the question here, as it may help someone. Weird...

Multiple server instances by using Hapi.js framework

I'm approaching to this framework for node.js for several reasons. Simplicity, great modularity and a fast configuration out of the box. I have soon encountered the concept of Pack which I have never seen during my experience in the learning of express.js framework. From the official guide the following example:
var Good = require('good');
server.pack.register(Good, function (err) {
if (err) {
throw err; // something bad happened loading the plugin
}
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
});
They says about Pack:
Packs are a way for hapi to combine multiple servers into a single
unit, and are designed to give a unified interface when working with
plugins.
This concept is weird for me. How many times do we work with different servers in a project? In addition is not clear for me whether I should call pack every time to register a plugins in hapi.
Update: This is pre v8 api code, the way to register a plugin has been changed. (call register directly on the server)
This concept is weird for me. How many times do we work with different servers in a project?
One example is when you have an api and a web server. These are usually developed separately, often in separate repositories. You could then create a third project which combines these plugins:
var Hapi = require('hapi');
var manifest = {
servers: [
{
host: 'localhost',
port: 8000,
options: {
labels: 'api',
cors: true
}
},
{
host: 'localhost',
port: 8001,
options: {
labels: 'web'
}
}
],
plugins: {
'./example-api': [{select: 'api'}],
'./example-web': [{select: 'web'}]
}
};
Hapi.Pack.compose(manifest, function(err, pack) {
pack.start();
});
In addition is not clear for me whether I should call ever time pack to register a plugins in hapi.
Yes, you need to call pack.register() when you want to register a plugin. However you can register more plugins at once:
plugin.register([
require('crumb'),
require('hapi-auth-cookie')
], function (err) {
// Error handling
}
Visit Link: http://cronj.com/blog/hapi-mongoose
Sample Project which can help you Repo Link: https://github.com/gauravgupta90/Hapi-Mongoose-Angular
For hapi version earlier than 8.x
var server = Hapi.createServer(host, port, {
cors: true
});
server.start(function() {
console.log('Server started ', server.info.uri);
});
For hapi new version
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: app.config.server.port });
Hapi.Pack() is no longer available in the newest versions of Hapi (8.x). They have pulled the functionality out into a small library called Glue. You can find it here: https://github.com/hapijs/glue. It works exactly how Hapi.Pack() used to.
By default, hapi supports multiple connections and you can customize them individually, like installing plugins only for a selected server (like API only).
You can instantiate and kick off multiple connection like this:
const hapi = require('hapi');
const port = 3000;
const _ = require('lodash');
// Create hapi server instance
const server = new hapi.Server();
// add connection parameters
server.connection({
host: 'localhost',
port: process.env.PORT || port
});
server.connection({
host: 'localhost',
port: process.env.PORT + 1 || port + 1
});
// Start the server
server.start(function () {
// Log to the console the host and port info
_.forEach(server.connections, function(connection) {
console.log('Server started at: ' + connection.info.uri);
});
});
Hope that helps.
If you need more details about that topic, you can find them within this tutorial on how to run separate frontend and backend servers within a single project

How to do socket.io implementation in Webrtc Video calling and what i have to change in the server.js?

How to do socket.io implementation in Webrtc Video calling?
A little bit overload but it works: SocialVidRTC
I understand from your question that you already have a WebRTC project and some signalling mechanism in server.js , possibly websockets .
To replace this with socket.io or any other signalling as SIP / XHR / AJAX etc , you need to replace server.js with new socket.io based code for request and response .
Follow these steps :
create a https server ( since webrtc pages capture web cam input only from secure origins) for socket.io. Assign server to an variable say app.
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('ssl_certs/server.key'),
cert: fs.readFileSync('ssl_certs/server.crt'),
ca: fs.readFileSync('ssl_certs/ca.crt'),
requestCert: true,
rejectUnauthorized: false
};
var app = https.createServer(options, function(request, response){
request.addListener('end', function () {
file.serve(request, response);
}).resume();
});
app.listen(8081);
here server.key , server.crt and ca.crt are fake ssl certs and 8081 is the https port I have selected .
you can reuse the same https server for hosting the webpages also.
listen on this same port for socket.io using app defined earlier
var io = require('socket.io').listen(app, {
log: false,
origins: '*:*'
});
io.set('transports', [
'websocket'
]);
I choose only websocket but you can set other types of transport too such as
socket.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
Now implement signalling specific functions and calls such as ,
io.sockets.on('connection', function (socket) {
...
socket.on('webrtc-joinchannel',function(data){
var resp=joinChannel(data);
socket.emit('resp-webrtc-joinchannel', resp);
});
...
});
Note : I am using socket.io v0.9 .
If yo want a example implementation you can view any sample projects such as here

Categories

Resources