use proxy pass xampp for socket.io node js server - node.js

I been trying to open the website from my mobile. I made two servers. One using xampp and php to run the website and get data from database. The second server is for running web socket. Real time chat and drawing. So server1 is in port 3000 and server2 is in 8000. how can I open both server in my mobile?
I tried
I put this in httpd.conf:
ProxyPass /node http://localhost:8000
and then in the client side, I put:
var socket = io('http://localhost/node', { transports : ['websocket']});
I am getting error:
WebSocket connection to 'ws://localhost/socket.io/?EIO=4&transport=websocket' failed:
node js setup:
const http = require('http');
const socket = require('socket.io');
const port = process.env.PORT || "8000";
const server = http.createServer((req,res)=>{
res.end('I am connected!');
});
const io = socket(server);
io.on('connection',(socket,req)=>{
socket.emit('Welcome','Welcome to the websocket server!!');
});
server.listen(port);
xampp setup:
$login = new Login();
$user_data = $login->check_login($_SESSION['Trial_User_Id']);
[xampp window][1]

Related

Node secure Websocket with IIS (Windows)

So I have created an application that uses a websocket in node.
In my server.js I use:
import http from "http";
import WebSocket from "websocket";
[...]
var httpServer = http.createServer(this.handleRequest);
httpServer.listen(port, function () {
console.log("Listening on port " + port);
});
var server = new WebSocket.server({httpServer: http_server});
[...]
This works, and creates a socket server on the same url.
Now, I'm trying to get this on a Windows server with IIS.
I start the application with "node server.js" and it is running on port 5003.
In IIS I use a rewrite rule to forward all incomming requests to the node server. Works perfect.
Now the problem. When I install an certificate with LetsEncrypt (Win-AMCE) the website is secure, but it won't connect to the websocket as the websocket is not secure.
According to some finds on the internet I need to use npm https
import http from "https";
import WebSocket from "websocket";
import fs from "fs";
[...]
const options = {
key: fs.readFileSync("my-site-key.pem"),
cert: fs.readFileSync("chain.pem")
};
var httpsServer = https.createServer(options,this.handleRequest);
httpsServer.listen(port, function () {
console.log("Listening on port " + port);
});
var server = new WebSocket.server({httpsServer: http_server});
[...]
The problem is, how do I get some valid certificate files. I cannot sign them self because another error will popup I guess. I cannot find the files Letsencrypt created.
So.... how do I create a secure websocket??

WebSocket is closed before the connection is established. Socket.io and React

I want to make an Chat application with Socket.io and I've followed this tutorial: https://youtu.be/ZwFA3YMfkoc. I am using React and Node.js
Everything works fine while using it locally and even on different devices on my Network. However if I am hosting my Backend on Heroku it doesn't work.
The error Message is:
WebSocket connection to 'URL' failed: WebSocket is closed before the connection is established.
(URL is the URL of my Backend with the Port). I am using SSL.
I've already tried to enable session affinity but it already was enabled.
My backend code is: (atleast the code that I think is important to the Problem)
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use(cors());
server.listen(PORT, () => console.log("Server started on " + PORT));
My frontend code is written in React and is:
var connectionOptions = {
"force new connection": true,
reconnectionAttempts: "Infinity",
timeout: 10000,
transports: ["websocket"],
};
const ENDPOINT = "URL";
socket = io(ENDPOINT, connectionOptions);
So I've fixed my Problem.
The URL on the client side had to be without the Port.
So for example:
const ENDPOINT = "https://web.site.com";
and not:
const ENDPOINT = "https://web.site.com:1337";
Call socket.connect() or just add autoconnect: true in options you are providing.

How to troubleshoot problem with setting WebRTC server on vps?

I wrote a simple node express server for webRTC using peerjs-server and simple client using peerjs. Everything works fine on localhost, but when I try it on vps, I get error:
Firefox can't connect with server ws://my.vps/peerjs/peerjs?key=peerjs&id=hj3hpekwaa38fr00&token=ymtfvhagiw
PeerJS: Socket closed.
PeerJS: ERROR Error: Lost connection to server.
Error: "Lost connection to server."
emitError https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:16426
_initializeServerConnection https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:12260
emit https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:25516
onclose https://cdnjs.cloudflare.com/ajax/libs/peerjs/0.3.16/peer.min.js:1:19350
Server:
const express = require('express');
enter code here`const app = express();
const ExpressPeerServer = require('peer').ExpressPeerServer;
app.use(express.static('./public'));
const server = app.listen(80, () => { // 3000 on localhost
console.log('Express server listen on port ' + 80);
});
const options = { debug: true };
const peerserver = ExpressPeerServer(server, options);
app.use('/peerjs', peerserver);
app.use('/*', express.static('./public/index.html'));
Client:
var peer = new Peer('', {
host: location.hostname,
port: location.port || (location.protocol === 'https:' ? 443 : 80),
path: '/peerjs',
debug: 3
});
peer.on('open', function (id) {
console.log(id);
});
Any help appreciate.
It looks like you are connecting with server ws://my.vps/, which is a web socket to a server at http://my.vps/ which doesn't seem to exist.
It should probably also be using https (or wss)

Node.js + Express.js + Socket.io on port 443 (HTTPS TLS/SSL)

I have an app with Node.js, Express.js, and Socket.io that runs fine using ANY port except 443. The server is meant to only operate over HTTPS port 443 and likewise, the websocket should be encrypted as well.
CODE THAT WORKS
var fs = require('fs');
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var sslOptions = {
key: fs.readFileSync(__dirname + '/../ssl/server.key,
cert: fs.readFileSync(__dirname + '/../ssl/server.pem,
ciphers: 'ECDHE-RSA-AES256-SHA:AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM',
honorCipherOrder: true
};
var app = express();
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, {
"log level" : 3,
"match origin protocol" : true,
"transports" : ['websocket']
});
server.listen(8443);
When I change the port (last line) to 443, the Node server crashes right away with an error:
warn: error raised: Error: listen EADDRINUSE
Apparently you've already got a server listening on that port on your machine. Is is possible that you started this server elsewhere and it's still running?
It means that the port is in use, you can check using :
sudo netstat -tapen | grep ":443".
If you use Apache, Ngnix or other server it is likely to be it.

Express 3.0 HTTPS

I have a Node.js Express 3.0 application which listens on port 3000 locally and 80 online, that's fine. What I need to do now however is introduce an SSL certificate.
I've looked at many sources online however they're all dated, or only work on port 443 or nothing. What I need to do however is listen on both 443 and 80 and re-direct any requests to 80 back to 443.
Are they any up to date examples of this?
I would do this with 2 distinct processes: an insecure proxy server and a secure server.
The insecure proxy listens on port 80 and responds to all requests with a 302 redirect to the secure server
Insecure Proxy
var http = require('http')
var port = 80
var server = http.createServer(function (req, res) {
// change this to your secure sever url
var redirectURL = 'https://www.google.com'
res.writeHead(302, {
Location: redirectURL
});
res.end();
}).listen(port, function () {
console.log('insecure proxy listening on port: ' + port)
})
Secure Server
var https = require('https')
var express = require('express')
var fs = require('fs')
var keyFilePath = '/path/to/key.pem'
var certFilePath = '/path/to/cert.pem'
var app = express()
// put your express app config here
// app.use(...) etc.
var port = 443 // standard https port
var options = {
key: fs.readFileSync(keyFilePath, 'utf8'),
cert: fs.readFileSync(certFilePath, 'utf8')
}
var server = https.createServer(options, app)
server.listen(port, function () {
console.log('secure server listening on port: ' + port)
})
Note that you could run both of these servers within a single process but it is more maintainable to separate the concerns into distinct processes.

Resources