Skip to content

Raigyo/in-the-heart-of-node

Repository files navigation

Node.js

February 2021

🔨 Node advanced concepts overview. From udemy 'Au coeur de Node'.

Node Logo

SYNCHRONOUS / ASYNCHRONOUS

Asynchronous I/O is a form of input/output processing that permits other processing to continue before the transmission has finished.

scripts
-- synchronicity
---- asynchronous_1.js
---- promises
---- synchronous
index-async.js

nodemon index-async.js

  • Variables scope
  • Synchronous functions
  • SetIntervall
  • Callbacks sync and async using setTimeout
  • Promises

Do not slow down the call stack!

  • Use async operations
  • Use events emitter

node-cycle

EVENT EMITTER / CUSTOM EVENTS

EventEmitter is a class that helps us create a publisher-subscriber pattern in NodeJS. With an event emitter, we can simply raise a new event from a different part of an application, and a listener will listen to the raised event and have some action performed for the event.

scripts
-- events
---- ShoppingList.js
index-emitter.js

nodemon index-emitter.js

  • Handler / Emitter
  • Custom events
  • Handler using class that inherits emitter
  • Functions that return an event emitter

STREAMS

Streams are one of the fundamental concepts that power Node. js applications. They are data-handling method and are used to read or write input into output sequentially. Streams are a way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient way.

scripts
-- streams
---- bigfile_copy.txt
---- bigfile.txt
---- fruits.txt
---- fruits_copy.txt
---- fruits_copy2.txt
index-streams.js

nodemon index-streams.js

  • Streams
  • Readable stream
  • Writable stream
  • Process object
  • Pipe
  • Back pressure management (drain, highWaterMark)

STREAMS WITH CLASSES

scripts
-- streams
---- CustomWritable.js
---- StreamText.js
index-streams-classes.js

nodemon index-streams-classes.js

  • Class heritating from readable
  • Class heritating from writable
  • Using buffers or objects

DUPLEX STREAMS / TRANSFORM STREAMS

Duplex streams are streams that implement both the Readable and Writable interfaces.

Transform streams are Duplex streams where the output is in some way related to the input. Like all Duplex streams, Transform streams implement both the Readable and Writable interfaces.

scripts
-- duplex
---- bigfile.txt
---- Slugify.js
index-duplex-streams.js

nodemon index-duplex-streams.js

  • Duplex streams using transform
  • Duplex streams using passtrough
  • Pipe with net (server with "echo")

SERVER WITHOUT STREAMS

Exemple with a video server and memory usage test.

scripts
-- video
---- sample_1920x1080.mp4
index-server.js

nodemon index-server.js

=> http://localhost:3000/video

==== route /video start ====
rss 103.91 Mb
heapTotal 4.68 Mb
heapUsed 2.67 Mb
external 74.09 Mb
arrayBuffers 72.96 Mb
==== route /video end ====

SERVER WITH STREAMS

Exemple with a video server and memory usage test.

scripts
-- video
---- sample_1920x1080.mp4
index-server-stream.js

nodemon index-server-stream.js

=> http://localhost:3000/video

==== Route /video start ====
rss 51.04 Mb
heapTotal 4.73 Mb
heapUsed 3.19 Mb
external 10.08 Mb
arrayBuffers 8.95 Mb
==== Route /video end ====

Useful links

About

Node advanced concepts overview.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published