A writable stream is one that you can write to but not read from. A readable stream is one that you can read data from but not write to.Ī good example of this is process.stdin, which can be used to stream data from the standard input. The most important concepts are that of Readable streams, Writable streams, Duplex streams, and Transform streams. The end event, which is emitted when there is no more data to be consumed from the stream.Īlso from Apress foundation book for node.js. The data event, which is emitted whenever the stream passes a chunk of data to the consumer.The most important events on a readable stream are: Once inside the event handler, you can call the read function on the stream to read data from the stream. This event is raised whenever there is new data to be read from a stream. Subscribing an event would be like : events/1basic.js var EventEmitter = require('events').EventEmitter var emitter = new EventEmitter() // Subscribe emitter.on('foo', function (arg1, arg2) ) The events module has one simple class “EventEmitter”, which we present next. As always, use require(‘events’) to load the module. Node.js comes with built-in support for events baked into the core events module. Maybe the client wants to know, maybe it doesn’t. This makes events ideal for scenarios where the significance of the occurrence is determined by the client. A component that raises events knows nothing about its clients, while a component that uses callbacks knows a great deal. Node.js EventsĪn event is like a broadcast, while a callback is like a handshake. Node.js has its async nature all around pretty much it would be considered as replacement option for this cumbersome method. Traditional way of reading input and writing response involved file loading in memory which would consume much more memory and it would be waste of resources.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |