socket.io ssl connection in node.js with express framework - javascript

I am quite confused about setting up a secure ssl connection on node.js and the bindings created with the socket.io.
It is giving big headaches, as it is constantly refusing to work, and the result in browsers tested are:
GET https://webrtc.romlex.info/socket.io/1/?t=1405428623632 404 (Not Found) socket error
My serverside code:
var options = {
key: fs.readFileSync(__dirname + '/public/video-phone/key.pem'),
cert: fs.readFileSync(__dirname + '/public/video-phone/cert.pem')
};
actually i am only initializing express
var app = express();
should i create server when initializing express, and pass options?
var app = express.createServer(options);
actually i am creating the https server with the options listening on secured port
var server = require('https').createServer(options,app).listen(443);
which way should i initialize socket.io connection?
Actually i am listening on server with options
var io = require('socket.io').listen(server,options);
Should i listen on server only?
var io = require('socket.io').listen(server);
Or should i listen on the secure port and options?
var io = require('socket.io').listen(443,options);
Quite a bit confused, please help!
i am using xhr-polling over websockets
io.set('transports', [
//'websocket',
'xhr-polling',
'jsonp-polling'
]);
the serverside code:
serverside socket.io with ssl
clientside code:
should i add secure param or not?
io.connect("https://webrtc.romlex.info", {secure: true});
Sorry for the questions, i'm quite a newbie on node.js, especially, when ssl connection comes to scene.
Thanks for your suggestions!

Related

Failed to load resource: the server responded with a status of 404 (Not Found) Node.js socket.io

I'm trying to create a server using Node.js and socket.io and it starts perfectly. However, when I'm trying to visit the site of my server through the browser, he writes "Cannot Get /" and in the console gives an error "Failed to Load Resource: The Server Respondd with A Status of 404 (Not Found)". For two days I'm trying to understand where the problem and I will be very grateful to your help. Here is my server code:
const express = require("express");
var http = require("http");
const app = express();
const port = process.env.PORT || 5000;
var server = http.createServer(app);
var io = require("socket.io").listen(server);
//middlewre
app.use(express.json());
io.on("connection", (socket) => {
console.log("connected");
console.log(socket.id, "has joined");
socket.on("/test", (msg) => {
console.log(msg);
})
});
server.listen(port, "0.0.0.0", () => {
console.log("server started");
});
Your server is currently not set up to handle any other http traffic than Websocket connection upgrade requests, which you can not make by entering a url in your browser. Also, the browser is not equipped to negotiate this connection upgrade or to keep the Websocket connection working once established. For this you need some javascript code to run on the client side.
The socket.io library provides everything you need, look at a minimally working example at the link provided below. You should basically just set up your server to serve an html document which provides a context from which the whole websocket connection upgrade can be managed - and the connection maintained - by the socket.io library.
https://socket.io/docs/v2#Minimal-working-example

why to make new instance of http for Socket.io when we already have express server?

I am new to SocketIO, I have referred many blogs and documentation for socket and everywhere we first need to create an HTTP server and then attach the socket to it like this -
var app = express();
var httpServer = http.createServer(app);
var io = socketio.listen(httpServer);
What does the second line mean? why are we creating one extra HTTP server while express(web framework) is already defined?
Because I never created a new HTTP instance for my RESTful application, I simply listened to express instance like this -
var express = require('express');
var app = express();
app.listen(8000);
Thanks in advance!
If you want socket.io to run on the same port as your web server, then you use the same server instance. If you want socket.io to run on a different port, then you create a new server instance on that port just for socket.io to use.
Socket.io works just fine using the same port and server instance as Express so unless you have a specific reason to run it on a different port, this is the usual way one would configure it.
Some code examples for socket.io show it in isolation by itself and thus they have to create an http server for it to use.
When using Express, you can get the server instance like this:
const express = require('express');
const socketio = require('socket.io');
const app = express();
const server = app.listen(8000);
const io = socketio(server);

Why heroku can't run WebSocket server without HTTP server?

I have a WebSocket app on heroku. I tried uploading my WebSocket app without routing http server but it doesn't work. What does mean server routing? And why can't heroku run a WebSocket server without an http server?
And why does it takes express object 'server' as argument in SocketServer
Here's my code of Websocket server.
const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express()
.use((req, res) => res.sendFile(INDEX) )
.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({ server });
wss.on("connection",function(ws){
ws.on("message",function(message){
if(message==='exit'){
ws.close();
}else{
wss.clients.forEach(function(client){
client.send(message);
});
console.log(message);
}
});
ws.send("welcome..");
});
There are too many questions in one. I hope my answers will address all of them.
First of all, the websocket protocol is an extension of http, it is not something different. Therefore, a websocket server is necessarily an http server, even though with extended capabilities.
Also, with socket.io, the websockets and http run on the same port. Therefore you have to open the port for http to have your websockets work.
Let's have a look at this line :
const wss = new SocketServer({ server });
It has a syntax error, it must be new SocketServer(server). Also, about the variable name, I would recommend not to use wss for a websocket over http, since wss stands for secure websockets and is related to websockets like http to https.
About routing in heroku: in heroku, you have to define which port your application is using. This can be compared to the firewall on your local machine : if you want to access it from outside, you have to open the port. On heroku, technically it is different to a firewall, but in this point it is similar.

Standalone socket.io and Websocket

I need to build a socket.io server that will intercept incoming connections from an app which is not stored in the same directory as the server.
The client side app does not contain node.js, thus I'm trying to use a websocket :
Telnet.Socket = new WebSocket('ws://127.0.0.1:3000');
My node.js server does not need a http server but must be a standalone socket.io app. Thus, I've tried the following code :
var io = require('socket.io')();
io.on('connection', function(socket){
console.log('connexion entrante');
});
io.listen(3000);
Unfortunately, the server part does not seem to get the Websocket connection request. My firefox says :
Firefox cannot establish a connection with the server at adress ws://127.0.0.1:3000/.
What am I missing ?
Thx in advance !
socket.io need client use socket.io to connect because it use many kind of connection. For connect websocket only you can use ws node module

Run NodeJS app on appFog

All I want to do is deploy my little nodeJS app onto the free hosting site, appFog.
Nomatter what ports I set on my client side or on my server side.. I consistently get the error message:
events.js:71
throw arguments[1]; // Unhandled 'error' event
^ Error: listen EADDRINUSE
When this is on my laptop / desktop running on localhost, everything works just fine.
So this is what I've got going on:
Client side:
this.connection = new WebSocket('ws://super1onate.aws.af.cm:1337');
Server Side:
var express = require("express"); // load the express module
var app = express(); // App now holds the server object
// What ports to listen on
app.listen(process.env.VCAP_APP_PORT ||1337);
server.listen(process.env.VCAP_APP_PORT || 1337, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort); });
Your server code looks ok. What is events.js? It looks like maybe you're including a module that's trying to bind to a port it shouldn't.
Once you get your server running, I don't think your client code will work. As far as I can tell, AppFog doesn't support websockets, and if it does, you'll probably want to hit port 80, not 1337.
Alright, I'm going to answer my own questions.
AppFog does not support WebSockets. websockets =/= socket.io btw fyi
Anyways, according to this site:
http://feedback.appfog.com/forums/171983-appfog/suggestions/3543100-add-websocket-support-to-node-js

Categories

Resources