Websocket help using NodeJS on Ionos VPS - node.js

Im a noob here and in the field of what I'm trying to do too. I have started to create a multiplayer game but it was using short polling and I don't think that is going to be best long term.
After completing a basic POC I started to rebuild and decided to use websockets as a solution.
I'm running a virtual private Linux server from Ionos
Plesk is installed
I have created a subdirectory called server in httpdocs which is where the app.js file is for server creation.
Port 8080 is open
Im using http/ws for now before I attempt to go near https/wss
I have enabled NodeJS on the server and set up the following as config:
Document Root: /httpdocs/server
Application URL: http://[my.url]
Application Root /httpdocs/server
Application Startup File app.js
The app.js contains:
var Msg = '';
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
console.log('Received from client: %s', message);
ws.send('Server received from client: ' + message);
});
});
On any web page that loads on that domain I now get an error stating 'Upgrade Required' error 426
I know that means the server wants the client to upgrade to websockets but I cant understand why the client isn't asking to upgrade back.. In my ws_test.html file I access to test the connection this is the code in the HTML body:
<script>
let socket = new WebSocket("ws://[my.url]/server/app.js");
socket.onopen = function(e) {
alert("[open] Connection established");
alert("Sending to server");
socket.send("TEST SUCCESSFUL");
};
</script>
I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.
I have tried so many different things and come to the end of my tether.. I have changed the connection URL many times and the ports. I suspect NGINX may have something to do with it but I tried disabling proxy and no change.
Can anyone see any glaringly obvoius mistakes!?
Thanks SO SO much for just reading this any help will be hugely appreciated.

Related

Socket.io client can not connect to localhost while it can connect to 127.0.0.1

I have a server set up using node.js and I'm trying to connect to the server with socket.io. The server used to work perfectly 2 months ago but for some reason it does not work anymore. The server code looks like the following:
var http = require('http');
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
var io = require('socket.io')(server);
io.sockets.on('connection', function (socket) {
console.log ('Client connected.'); });
console.log ('Server started.');
server.listen(3000, '0.0.0.0');
On my client side, I'm using the line
socket = io.connect("http://localhost:3000");
to try to connect to my server, but it doesn't work. The connection works perfectly if I use the line
socket = io.connect("http://127.0.0.1:3000");
If I type localhost:3000 on the browser, it sends me to the page saying Hello World as intended, same as 127.0.0.1, so it is not working only when I'm using io.connect.
Things that I have tried are:
changing the host file (adding the line 127.0.0.1 localhost, removing the line including ::1, making a new host file and overwriting it)
trying it on a different machine, still the same problem
resetting the router settings.
pinging localhost on a cmd prompt, it says reply from ::1: time<1ms
installing different version of socket.io and node.js (both the most recent ones and versions 2 months ago)
Considering what happens from web browser, localhost seems to work but it only does not work when I use socket.io with it. I have been unable to solve this problem that suddenly appeared, so I would appreciate it a lot if someone can give me some insights on what can be causing this.
It seems like an IPv4/IPv6 issue. You may try changing this:
server.listen(3000, '0.0.0.0');
to this:
server.listen(3000, '::1');
or this:
server.listen(3000);

Node.js + ws - websocket is unexpectedly closing

I have a Node.js + ws server and a Qt client. I'm having an issue with the websocket connection closing unexpectedly in certain conditions. In my client, I iterate through a list of objects and send a series of requests to the server. The server then accesses a database and then writes the response back to the client. Here is the (simplified) basic server loop:
// ...
var httpServer = http.createServer(//params);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: httpServer });
// ...
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(msg) {
handleRequest(JSON.parse(msg), function (err, result) {
if (err) throw err;
ws.send(JSON.stringify(result), function ack(error) {
if (error) throw error;
}
}
});
});
If I limit the number of request from the client, everything works fine. But if I reach a critical number of requests, data stops coming back from the server and the websocket connection times out.
I'm wondering if I need to flush the websocket on the server before or after each write? QtWebSocket has a flush() method, but I don't see this capability in the ws docs. Any ideas would be appreciated.
UPDATE: I should also note that no errors are sent to either ws or wss. Data just stops flowing.
UPDATE 2: Okay, this seems pretty clearly to be a problem with transmitting oversized packets from my client over the Internet to the node+ws server. Based on Wireshark traces, once the oversize threshold is reached, the socket craps out, but only when I'm connected remotely to the server. If the server is on my local subnet, oversized packets are properly transmitted to the server. Any ideas how to debug this?
This problem resolved after I replaced one of my routers. I am chalking this up to a hardware issue with the old router. This was consumer-grade hardware, so I'm hoping this shouldn't be an issue in production. It does make me wonder how many of these kinds of bugs might be hanging out on the Internet...

Websocket server running fine but cannot connect from client (what url should I use?)

OK this is very simple to anyone who's used websocket and nodejs.
I have created a websocket server named ws_server.js and put it in C:\Program Files (x86)\nodejs where I have installed the nodejs framework. I started the server and it is running and it says it's listening on port 8080. So far so good, I have the server running.
Now I simply want to connect to it from client code so that I can do all that lovely stuff about capturing events using event listeners etc. The problem is, embarassingly, I cannot figure out what URL to use to connect to my websocket server.
function init() {
testWebSocket();
}
function testWebSocket() {
websocket = new WebSocket("ws://localhost:8080/"); // WHAT URL SHOULD BE USED HERE?
websocket.onopen = function(evt) { alert("OPEN") };
websocket.onclose = function(evt) { alert("CLOSE") };
websocket.onmessage = function(evt) { alert("MESSAGE") };
websocket.onerror = function(evt) { alert("ERROR") };
}
function doSend(message) {
// this would be called by user pressing a button somewhere
websocket.send(message);
alert("SENT");
}
window.addEventListener("load", init, false);
When I use ws://localhost:8080 the only events that trigger are CLOSE and ERROR. I cannot get the client to connect. I must be missing something very simple. Do I need to set up my nodejs folder in IIS for example and then use that as the URL?
Just to reiterate, the websocket server is running fine, I just don't know what URL to use to connect to it from the client.
EDIT: The websocket server reports the following error.
Specified protocol was not requested by the client.
I think I have got it working by doing the following.
var websocket = new WebSocket("ws://localhost:8080/","echo-protocol");
The problem being that I needed to specify a protocol. At least now I get the onopen event. ...if nothing much else
I was seeing the same error, the entire web server goes down. Adding the protocol fixes it but leaves me wondering why it was implemented this way. I mean, one bad request should not bring down your server.
You definitely have to encase it a try/catch, but the example code provided here https://www.npmjs.com/package/websocket (2019-08-07) does not. This issue can be easily avoided.
I just wanted to share a crazy issue that I had. I was able to connect to a websocket of an old version of a 3rd party app in one computer, but not to a newer version of the app in another.
Moreever, even in new computer with the new version of the app, The app was able to connect to the websocket, but no matter what I did, when I tried to connect with my own code, I kept getting the error message that the websocket connection failed
Long story short, They changed an apache configuration that allowed connecting to the websocket via a proxy.
In the old version, apache config was:
ProxyPass /socket/ ws://localhost:33015/ retry=10
ProxyPass /socket ws://localhost:33015/ retry=10
In the new version, apache config was changed to:
ProxyPass /socket/ ws://localhost:33015/ retry=10
By bad luck, I was trying to connect to ws://localhost/socket and not to ws://localhost/socket/. As a result, proxy was not found, and connection returned an error.
Moral of the story: Make sure that you are trying to connect to a websocket url that exists.
For me, the solution was to change the URL from ws:// to wss://. This is because the server I was connecting to had updated its security, and now only accepted wss.

Connecting to socket.io server

I'm trying to get this exmaple working on my own computer:
https://github.com/tamaspiros/simple-chat
I have node.js installed and have installed socket.io as well. The readme is informing me to change the IP addresses on these two lines:
var socket = io.listen(1223, "1.2.3.4");
var socket = io.connect("1.2.3.4:1223");
However, I'm not really sure what to change the IP addresses into. I would like to get the simple chat box application working on my own computer.
You just need to configure it in one like, for ie:
var socket = io.connect('adress:port');
where addres is Your socket.io server IP or hostname, and yyyy is port on which it listens.
after that, You can get and emit events with:
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
on server listen for
and thats it.
Before making chat You can try just using this example from official socket.io: link
PS dont forget to add socket.io client file source on client side! :). The whole code should look like in the example in the provided link.

Adobe AIR- connection issues with socket.io

I have a desktop app in Adobe Air (flex).
I have used flashSocket.io library in my app to communicate with socket.io on node.js server.
It works perfectly for most of the clients.
But for some random client the flex app is not able to create a connection with the socket.io server. It constantly throws connection error and close error on flex. The clients are not behind any firewalls or proxy server.
The console has warning like
Web Socket Connection Invalid
I guess this for those clients who are not able to connect.
Since its working for majority of the users i don't know where should i look into. Also, i am unable to reproduce this on my side.
Here's the Server Code:
var io = require('socket.io').listen(app);
app.listen(8080, function() {
console.log('%s listening at %s', app.name, app.url);
});
io.configure(function() {
io.set('transports', ['flashsocket']);
io.set('flash policy port', 843);
});
Flex code:
socket = new FlashSocket("http://domain.com:8080/");
socket.addEventListener(FlashSocketEvent.CONNECT, onConnect);
socket.addEventListener(FlashSocketEvent.MESSAGE, onMessage);
socket.addEventListener(FlashSocketEvent.CLOSE, onDisconnect); //only close and connect_error event is always fired for certain clients
socket.addEventListener(FlashSocketEvent.CONNECT_ERROR, onConnectError);
Any help would be highly appreciated.
Use a different port, we're using 443.

Resources