Node, Express, Socket.io Unable to Connect To Server - node.js

I have made a node server with with express and sockets io. It is a simple chat application that would be an addon, to my existing project on my website.
I am having this issue where I am unable to connect to the server from the client to be able to use the chat application.
Error: polling-xhr.js:202 GET https://128.127.106.17:6379/socket.io/?EIO=4&transport=polling&t=Nixndgr net::ERR_CONNECTION_TIMED_OUT
SERVER
const fs = require("fs");
const path = require('path');
const express = require('express');
const app = express();
const http = require('http');
const options = {
key: fs.readFileSync(PATH_TO_KEY),
cert: fs.readFileSync(PATH_TO_CERT),
}
const server = http.createServer(app);
const cookie = require('cookie');
const { Server } = require("socket.io");
const io = new Server(server);
CLIENT
var socket = io(HOSTNAME, {'transports': ['websocket', 'polling']});
With these settings it works perfectly fine on my local environment, but with the actually website I have trouble getting it connected.
The few things I have attempted
I tried adding the ssl options , "const server = http.createServer(options, app);"
I played around with CORS as well and different settings.
Found this https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/
And I found that the server was running and I would get the hello world but still not able to connect to it and starting to get stumped and running out of resources/possible solutions.

Related

socketio, client can connect to server if hosted locally, but returns error when trying to connect to server hosted on seperate vserver

I am trying to make a simple server with socket.io and express and connect to it through a website.
when i followed a tutorial on socketio with localhost, everything worked fine, but when i put the server on a vserver, and tried to connect to it, i got this error:
Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR
as well as:
GET https://54.53.0.254:47185/socket.io/?EIO=4&transport=polling&t=O09jjrs net::ERR_SSL_PROTOCOL_ERROR
here is my server code:
const express = require('express');
const app = express();
const server = app.listen(47185);
const socket = require('socket.io');
const io = socket(server)
console.log('server running on port 47185');
io.sockets.on('connection', newConnection);
function newConnection(socket) {
console.log('new connection: ' + socket.id);
socket.on('input', inputLog)
function inputLog(data) {
socket.broadcast.emit('input', data);
console.log(data);
}
}
and here is my client code (this is all that relates to socket.io, the rest is just for the website)
var options = {
rejectUnauthorized:false
}
var socket;
socket = io.connect('89.58.0.199:47185', options);
socket.on('input', foreignInput)
function foreignInput(data) {
terminal_animate('\n' + data)
}
i have tried many different fixes and googled everything i can think of, and i'm just not sure what the problem is.
can anyone help me out with this issue? thanks in advance.
In the documentation, according to the Client Initialization part, in node.js you should provide the protocol when connecting to the server.
// the following forms are similar
const socket = io("https://server-domain.com");
const socket = io("wss://server-domain.com");
const socket = io("server-domain.com"); // only in the browser when the page is served over https (will not work in Node.js)
The first two example shows the secure https/wss as protocol, for that you need to serve the required files from the server, example in the documentation.
With http/ws as protocol it should work, but the communication will not be secure.
The Server Initialization / With Express shows an example to call .listen on the return value of createServer from the http module, with the app given as a parameter.
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, { /* options */ });
io.on("connection", (socket) => {
// ...
});
httpServer.listen(3000);
With a caution that says:
Using app.listen(3000) will not work here, as it creates a new HTTP server.

express http client connect to server , https client can't connect to server

I've made a server that works with http just fine. The server was up and I was able to connect to it with Chrome and Postman. When I switch the server to https, the server is up, but I can't connect to it with Chrome and Postman. the ssl keys were sign by certbot.
server.js
const https = require('https');
const app = require(__dirname+'/app');
const fs = require('fs');
const port = 80;
const options = {
cert: fs.readFileSync("ssl/v2/fullchain.pem"),
key: fs.readFileSync("ssl/v2/privkey.pem")
}
https.createServer(options, app).listen(port);
console.log(port);
app.js
const express = require('express');
const morgan = require('morgan');
const body_parser = require('body-parser');
const homepage = require(__dirname+'/routes/homepage');
const user = require(__dirname+'/routes/user');
const test = require(__dirname+'/routes/test');
const table = require(__dirname+'/routes/table');
const catalog = require(__dirname+'/routes/catalog');
const cart = require(__dirname+'/routes/cart');
const payment = require(__dirname+'/routes/payment');
const app = express();
app.use(morgan("dev"));
app.use(body_parser.json());
app.use(body_parser.urlencoded({extended: true}));
app.use("/", homepage);
app.use("/user", user);
app.use("/test", test);;
app.use("/table", table);
app.use("/catalog", catalog);
app.use("/cart", cart);
pp.use("/payment", payment);
module.exports = app;
... the server is up, but I can't connect to it with Chrome and Postman
It is not clear from your description how exactly you are trying to connect to the server but I assume that you'll try a simple https://example.com/.
const port = 80;
...
https.createServer(options, app).listen(port);
But based on your code you are trying to use HTTPS on the port reserved for plain HTTP (80) instead of using the default port for HTTPS (443). Thus, https://example.com/ will not work since this will try to use port 443 and you would need to explicitly specify a different port with https://example.com:80/. But the better option would of course to use the default port for HTTPS in the first place in your code, i.e. 443 instead of 80.

Using sockets with express in production environment(heroku)

While working with localhost I used the following code to implement socket functionality-
Server Side
var express = require('express');
var app = express();
const io = require('socket.io')(5000);
Client Side
<script src="http://localhost:5000/socket.io/socket.io.js"></script>
<script>const socket = io.connect('http://localhost:5000');</script>
The above code worked fine for me locally. Earlier tried use the code given in the official documentation, but it did not work. I got a 404 error when trying to load socket.io.js file and it said 'io is not defined' on the client side. That code is given below-
Server Side
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
Client Side
<script src="/socket.io/socket.io.js"></script>
<script>var socket = io.connect('http://localhost');</script>
Now I need to deploy the app to Heroku and don't know how to do the socket functionality. I tried both the above ways but none worked in Heroku. I am getting '404 error' and 'io is not defined'. The server-side code is written in ./app.js and client-side code written in ./public/index.html.
How do I get this socket functionality to work in Heroku and also why is the official code not even working locally?
Trying to get the same thing working, don't have all the answers but on heroku you need to grab the port from the environment variable.
// Needed for Heroku serving
if (process.env.PORT > 0)
port = process.env.PORT;
else
port = 5000;
// create the express server (app) and the socket io server (io)
const app = express();
const server = app.listen(port, () => {
console.log("Listening on port: " + port);
});
const io = require('socket.io')(server);
Client Side
const socket = socketIOClient('APPNAME.herokuapp.com:80');
socket.emit("MsgType", "Payload")

node.js socket io https

I'm trying to establish a WebSocket (with node.js/socket.io) on an
HTTPS protocol. But the client still keeps on polling and could not find the server although the server seems fine and listens.
This is what I have done so far. Could you see anything wrong with it?
My assumption is there is something not right with the certificates which I am using. I encrypted the server with plesk "lets encrypt" and took the certificates from this procedure is that right?
---- server side ----
var fs = require('fs');
var express = require('/opt/plesk/node/7/bin/node_modules/express');
var https = require('https');
var app = express();
var server = https.createServer({
key: fs.readFileSync('file.pem'),
cert: fs.readFileSync('file.crt')
},app);
var io = require('/opt/plesk/node/7/bin/node_modules/socket.io').listen(server);
server.listen(8080);
---- client side -----
var socket = io('/', {rejectUnauthorized: false, secure:true});
This is what the client gives me continuously:
https://foo.de:8080/socket.io/?EIO=3&transport=polling&t=MF9zjE6
Since you don't tell what error message you get it's a little hard to identify your problem. Perhaps you should try getting your socket to work and then afterwards try to implement https.
Maybe you should try and simplify it a bit like this.
Server:
var express = require('express')
var app = express()
var server = require('http').Server(app)
var io = require('socket.io')(server)
server.listen(8080);
Client:
var socket = io();
This piece of code should create your socket and your express app. Then you could try and change http to https and it should work aswell.

How to get certificate file from server?

When i dont get any proper answer so i asked this question that i created a chat app for my site using socket.io and node.js my site is on https server it's not working it's giving me error
GET https://example.com:3000/socket.io/?EIO=3&transport=polling&t=1507034613131-2 net::ERR_INSECURE_RESPONSE
CODE IS HERE
var app = require('express')();
var fs = require('fs');
var path = require('path');
var forceSsl = require('express-force-ssl');
app.use(forceSsl);
var options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
var server = require('https').createServer(options, app).listen(3000,function(){
console.log("Https server started on port 3000");
});
var io = require('socket.io').listen(server);
i find above code for https authentication so how can i get these files i have already installed ssl.
i think you can check in your cpanel there is ssl manager which is cpanel service so you can get from there after that you can use in your https nodejs service

Resources