Introduction to Node.Js
What is Node.Js?
- a.k.a. server-side solution for JS
- Compiles JS, making it really fast
- Runs over the command line
- Designed for high concurrency
- Without threads or new processes
- Never blocks, not even for I/O
- Uses the CommonJs framework
- Making it a little closer to a real object oriented language
- Non Blocking I/O
- Single Thread with Event Loop
- 40,025 modules
- Windows, Linux, Mac
- 1 Language for Frontend and Backend
- Active community
Concurrency: The Event Loop
- Instead of threads Node uses an event loop with a stack
- Alleviates overhead of context switching
Thread Vs. Event Driven
- Thread lock application/request with listener-workers threads, Whereas in event driven its only one thread, which repeatedly fetches an event.
- Thread using incoming-request model, Whereas event using queue and then processes it.
- In thread it uses multithreaded server that might block the request which might involve multiple events, Whereas in event driven it manually saves state and then goes on to process the next event.
- Thread using context switching, Whereas in event there is no contention and no context switches.
- Thread using multithreading environments where listener and workers threads are used frequently to take an incoming-request lock, Whereas event is using asynchronous I/O facilities (callbacks, not poll/select or O_NONBLOCK) environments.
NodeJs Vs Apache
- It’s fast
- It can handle tons of concurrent requests
When to use it?
- Real-time Applications
- Intelligent Proxies
- High Concurrency Applications
- Communication Hubs
- Web application
- Websocket server
- Ad server
- Proxy server
- Streaming server
- Fast file upload client
- Any Real-time data apps
- Anything with high I/O
- And many more …