Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write After End error in node.js webserver

I am struggling with my node.js hobby project due to a "write after end" Error. I have a created a node.js webserver that amongst other things, sends commands received from a HTML page onwards to another process using the following code:

var netSocket = require('net').Socket(); netSocket.connect(9090); netSocket.write(messages); netSocket.end(); 

This works until the traffic starts to increase (i.e. the amount of messages being sent and or the size of the messages). At this point I get the following error:

Error: write after end     at writeAfterEnd (_stream_writable.js:132:12)     at Socket.Writable.write (_stream_writable.js:180:5)     at Socket.write (net.js:615:40)     at Socket.<anonymous> (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/speech_module/web_server_HTTPS.js:66:15)     at Socket.emit (events.js:95:17)     at Socket.onevent (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/socket.js:327:8)     at Socket.onpacket (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/socket.js:287:12)     at Client.ondecoded (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/lib/client.js:193:14)     at Decoder.Emitter.emit (/Users/mark/Documents/GitHub Repos/voice_controlled_zumo/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20) 

My guess is that the server at 9090 is being overwhelmed by the amount of traffic, giving rise to the error. As a complete newbie in the node.js world I'd really appreciate any hints for how I could resolve this issue.

Note also that the webserver is serving pages over SSL (in case that makes any difference).

Thanks for taking the time to read this!

Mark

like image 789
Mark Avatar asked Jan 04 '15 20:01

Mark


People also ask

How do I write at the end of a node js file?

For occasional appends, you can use appendFile , which creates a new file handle each time it's called: const fs = require('fs'); fs. appendFile('message. txt', 'data to append', function (err) { if (err) throw err; console.

How do you end a response in node JS?

end() method ends the current response process. This method is used to quickly end the response without any data.

What is type error in node JS?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.

How does node js handle type errors?

Within the Node framework a TypeError indicates that a passed argument is not of the appropriate type. This method is used liberally throughout the built-in Node API modules, and should also be used within your own custom code to perform type checking at the top of of your functions and methods.


1 Answers

NodeJS is a non-blocking async platform.

In your case,

netSocket.write(messages); 

is an async method; therefore, netSocket.end() is called before write is complete.

The correct use would be:

netSocket.write(messages, function(err) { netSocket.end(); }); 

The second argument here is a callback function that will be called once the 'write' method finishes its job.

I would recommend you read/watch more about NodeJS, async styles and callbacks.

Here is a great place to start: https://www.youtube.com/watch?v=GJmFG4ffJZU

And of course, the NodeJS API docs regarding net sockets.

like image 101
Ron Bar Avatar answered Oct 12 '22 15:10

Ron Bar