Socket.io connection breaks on "socket.emit()" in some cases - node.js

I am building an app with node.js as server and vue.js in frontend with this package https://github.com/MetinSeylan/Vue-Socket.io
I am sending a lot of data from the server to the client (inital on connection)
Node.js loads the data from mongodb.
So in some cases, when I do a
socket.emit()
the connection breaks up (with no error) and does instantly a reconnect. And this in an infinit loop.
I found out, that the problem was once caused by german "Umlauts", for example "ä, ö, ü", etc. I fixed this issue by doing this encodeURIComponent(JSON.stringify(myDataToSend))) and in the frontend decode it like this: JSON.parse(decodeURIComponent(payload))
So now this doesnt help anymore and I cant figure out why. And there is no kind of error log or something where I can start debugging, it just breaks up and reconnects forever.
Can this happen because my Object I want to send to the client is too big?
I tried sending my data in smaller packages one after another and this did not break. I hoped I would find the problem like this.
Any advice for debugging or ideas for packing my data is appreciated.

I found the issue wich caused my problem.
Note: I have still no idea, why the socket connection crashed in my case, but I used the package in the "wrong way".
I did:
Vue.use(VueSocketio, 'http://socketserver.com:1923', store);
but the kind of better way ist
Vue.use(VueSocketio, socketio('http://socketserver.com:1923'), store);
vue-socket.io uses a very old version of the socket-io.client when just using a string as connection and no actual socketio instance.
Maybe this helps someone else

Related

how to detect connection failed in node js?

I use net.connect to make socket connection, I wonder how to detect it when a connection has failed?
It seems this doesn't work
//this will return a net.Socket and automatically connect
var client = net.connect({port:22000, host:'10.123.9.163'});
//doesn't trigger a error event even if connection fails
client.on('error', (err)=>{console.log('something wrong')});
//now an error event is emitted reasonably
client.write('hello');
when I run this piece of code, the connection should fail, and it indeed fails because when I write some data, an error occurs. But, I can not detect the connection failure. How can I do that?
=====Ready to close======
God damn it, I think I have just make a mistake. In fact the connection succeeded but due to some security strategy the server close the connection, I find out by doing a telnet. After trying other port which should definitely fail, the error event is emitted, everything go normal as expected. So, I am gonna close this question in case of misleading other people, and also thank you guys for helping me :)
The easiest and most portable way is to simply implement a 'ping-pong' check where both sides send some kind of 'ping' request every so often. If n outstanding ping requests go unanswered after some period of time, then consider the connection dead. This kind of algorithm is basically what OpenSSH uses for example (although it's not enabled by default).

NodeJS request pipe

I'm struggling with a technical issue, and because of I'm pretty new on NodeJS world I think I don't have the proper good practise and tools to help me solve this.
Using the well known request module, I'm making a stream proxy from a remote server to the client. Almost everything is fine and working properly until a certain point, if there is too much requests at the same time the server does no longer respond. Actualy it does get the client request but is unable to go through the stream process and serve the content.
What I'm currently doing:
Creating a server with http module with http.createServer
Getting remote url from a php script using exec
Instanciate the stream
How I did it:
http://pastebin.com/a2ZX5nRr
I tried to investigate on the pooling stuff and did not understand everything, same thing the pool maxSocket was recently added, but did not helped me. I was also seting before the http.globalAgent to infinity, but I read that this was no longer limited in nodeJS from a while, so it does not help.
See here: https://nodejs.org/api/http.html#http_http_globalagent
I also read this: Nodejs Max Socket Pooling Settings but I'm wondering what is the difference between a custom agent and the global one.
I believed that it could come from the server but I tested it on a very small one and a bigger one and it was not coming from there. I think it definitely coming from my app that has to be better designed. Indeed each time I'm restarting the app instance it works again. Also if I'm starting a fork of the server meanwhile the other is not serving anything on another port it will work. So it might not be about ressources.
Do you have any clue, tools or something that may help me to understand and debug what is going on?
NPM Module that can help handle stream properly:
https://www.npmjs.com/package/pump
I made few tests, and I think I've found what I was looking for. The unpipe things more info here:
https://nodejs.org/api/stream.html#stream_readable_unpipe_destination
Can see and read this too, it leads me to understand few things about pipe remaining open when target failed or something:
http://www.bennadel.com/blog/2679-how-error-events-affect-piped-streams-in-node-js.htm
So what I've done, i'm currently unpiping pipes when stream's end event is fired. However I guess you can make this in different ways, it depends on how you want to handle the thing but you may unpipe also on error from source/target.
Edit: I still have issues, it seams that the stream is now unpiping when it does not have too. I'll have to doubile check this.

User not disconnecting in Socket.IO Node.Js app ONLY on https connection - caching issue?

I'm having this issue with my Socket.Io / Node.Js app that when I reload a page Socket.IO still thinks that the "previous" me is connected to the chat room, so it thinks there's two people in the chat room instead of one. After a while (maybe like a minute) this problem disappears.
I tried resolving it but I think there's some issue with cache - that even though I reload the browser window the previous session is still active, so it "thinks" two people are connected.
This issue doesn't occur at all times, but always on HTTPS connection, and almost always on iOS and sometimes in Safari / Chrome and other browsers (on all systems with https connection).
Do you know what the issue might really be and what would be the best way to resolve it?
I use all the standard code for setting up the Socket.IO connection and the app is running on Express / Node.Js.
I can put the code here, but it's quite a lot, the open source code is available on http://github.com/noduslabs/infranodus (the app.js file and public/entries.js is where the socket.io code is).
Thank you!

Socket.io client-side reconnect not working

I'm using Socket.io to communicate between my Node.js server and the client website. Everything works great! Except one thing.
I'm allowing the user to disconnect from Socket.io and reconnect later, or at least I'm trying to. Keeping in mind that my socket from calling io() is stored in mySocket, disconnecting works fine using mySocket.io.disconnect(). The server catches it, the client knows it's disconnected, it's great. The issue is when I try to reconnect later...
So at first I just tried to set mySocket=io() again, but that failed. I learned there's a reconnect method, so I tried using that, and my problems got worse. So now I'm using mySocket.io.reconnect(), and I'm getting some weird results, described below.
So first of all, the server IS getting the reconnection attempt. It's catching the on('connection',...) event just fine. However, the CLIENT seems to think it's never reconnected--for example, the value of mySocket.connected stays at false and all emit() calls fail (and never get to the server).
Is there some special method I'm supposed to use besides simply mySocket.io.reconnect()? Or some variable or argument I'm supposed to set? Why is my server catching the reconnect if the client thinks it's not connected? What's going on and how do I fix it?
In Client sode:
mySocket = io('/', {forceNew: true}) helped me in similar situation.

Web socket & Flash socket clients connect to one Node.js

I've got problem connecting Flash client to Node.js server.
Short story:
For a first time I'm building a Node.js server that should be used by both web client (WebSocket) as well as a Flash client (Socket). The web client, of course, works like a charm, but I can't get over the Flash one. I get SECURITY_ERROR. After a day of research I think it's because of the policy file not being loaded. Ideas (primus on top of engine.io) ?
Long story:
I'm using Primus as I thought I'll need it because I have both web sockets and flash sockets to handle. Not sure if this is accurate? :)
I'm using Engine.io as a 'transformer/transporter' - the main framework that the layer uses. I won't discuss the standard web client (using Chrome and primus-client), as it's easy to setup.
I'm using simple and standard Sockets in AS3:
_socket = new Socket();
_socket.addEventListener(Event.CONNECT, onSocketConnect);
//...
_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
_socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
_socket.connect('localhost', '1337);
When building it within Flash IDE, it goes to the onSocketConnect function, but if I try to write anything to the socked - I get disconnected. If I run this from the web browser, I get into the onSecurityError method.
I must say that I don't get any traces in the node console!
primus.on('connection', function connection(spark) {
console.log('new connection'); // never gets logged!
As I know, security error is thrown when there is error with the policy file, so I started searching for a solution for that.
I've read a lot of things online, and most common solution was simple usage of socket.io and so called FlashSocket.IO. I tried implementing it, but it's so old, that some of the code is a kind of missing and I finally got some errors from the hurlant library - I couldn't get it working.
I also saw some node package called policy, which runs separate server to server the policy file.
I tried adding a transport array with flashsocket in it - no change. I also can't understand why all of the samples are using transports - I've searched and both index.js and primus.js are using transport (why there are two separate files, Jesus?!)
I could try using only engine.io without primus, but I don't know if this would be of any help. All the posts and samples I've found are pretty old - please help me with any up to date solution or at least some explanation what needs to be done - seems like a whole new universe to me :)
Thanks in advance!
Edit:
Thanks to the The_asMan, I figured out it has something to do with the handshake. I've tried this simple example (despite the fact it's so old) - it worked perfectly for the Flash client! Of course I cannot connect web sockets to it, as the handshake is not proper - it has some kind of protocol for it.
So I guess I just have to understand how to get the <policy-file-request/> in node - I'll be able to return the policy file. But I don't know how to get it - I don't receive any kind of data nor connect handler...
You have a cross domain policy issue.
I answered it all here.
AS3 - Flash/AIR Socket Communication writeUTFBytes only works once
just an idea:
On some operating systems, flush() is called automatically between execution frames, but on other operating systems, such as Windows, the data is never sent unless you call flush() explicitly. To ensure your application behaves reliably across all operating systems, it is a good practice to call the flush() method after writing each message (or related group of data) to the socket.

Resources