First time asking a question here (Stackoverflow is getting too toxic for me) and hopefully someone will be able to help me.
I am working on a communication program meant to transmit large chunks of data. However, it fails at exactly that - large chunks of data. I narrowed it down to exactly 1538 characters being transmitted before someone going wrong.
I am under the impression that NodeJS reads the data before it finished transmitted.
Code (simplified):
socket.on('data', function(data) {
data = data.toString(); //This is 1538 characters
plain = decrypt(data).toString(); //This is 724 characters. It is JSON in text format
json = JSON.parse(plain); //This throws error
/* SyntaxError: Unexpected end of input
JSON is *obviously invalid as the last curly bracket is missing (as well as extra data)
*/
I’m not sure if I can help, but just to be clear data = data.toString(); data is 1538 characters here and plain = decrypt(data).toString(); plain is being truncated here?
If that’s the case, it sounds like something is going on in the decrypt, not the connection.
And are you sure the data being transmitted in a normal string and not a JSON string? What about data = JSON.parse(data) instead of .toString()? Or if it is being transmitted in JSON, it could be something as simple as decrypt(data.fieldToBeDecrypted) instead of the other stuff.
Also, what libraries are you using for the socket connection and decrypt functions?
The encryption algorithm increases the total amount of characters. The limit is hit with 1538 characters BEFORE being decrypted. Encryption algo is fine.
I am now doing a terribly hacky solution. I am appending all data received to itself until one of the pseudo-packets ends with a dollar sign. The chances that it ends in a dollar sign are very slim so I go with that. (Terrible practice, I know, i’m sorry)
However, it IS working. This does prove that the crypto works fine.