Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
NodeJS Chapter-2
26 Dec 2020
Agenda
• Authentication and Authorization
• Worker Threads
• Web Sockets
• Streaming
• Cluster mode
• Database connection
2
Authentication
Authentication is the act of validating that users are whom they
claim to be. There are below ways to do authentication
• Username/Passwords
• Usernames and passwords are the most common authentication
factor. If a user enters the correct data, the system assumes the
identity is valid and grants access.
• One-time pins
• Grant access for only one session or transaction.
• Authentication apps
• Generate security codes via an outside party that grants access
• Biometrics
• A user presents a fingerprint or eye scan to gain access to the
system.
• We will discuss about Json Web Tokens for
authentication.
• JSON Web Tokens (JWT) have been introduced as
a method of communicating between two parties
securely.
• JWT is very popular for handling authentication
via HTTP.
• It keeps user identify within the token and transfer
token with http request.
Authentication
Authentication
Client Server
Client Server
Login Request
Signed JWT
Data request + Signed JWT
Response
Authentication
There are three section of JWT token as mentioned below,
• Header – First section is header, which is base 64
encoded string
• Data(Payload) – It contains JSON object send back to
User. It is recommended not to include any sensitive
data in JWTs, such as password.
• Signature of Token - This is generated by hashing the
string base64UrlEncode(header) + "." +
base64UrlEncode(Data) + secret using the algorithm
that is mentioned in the header section.
Authorization
• Authorization is a process the process of
grating/denying access to resource which allows the
user access various resources based on the user’s
identity.
• In Nodejs application, there could be different
functionalities/APIs those can be accessed by different
type of users depending on the role and
granting/denying access as per the role is the
authorization.
• There are different ways to implement authorization.
E.g. role based, oauth2., however we will discuss here
role based authorization.
8
• The worker_threads module enables the use of
threads that execute JavaScript in parallel.
• Workers (threads) are useful for performing CPU-
intensive JavaScript operations.
• worker_threads can share memory.
• They will not help much with I/O-intensive work.
Node.js’s built-in asynchronous I/O operations are
more efficient than Workers can be.
Worker Threads
Worker Threads
9
• worker_threads became available in Node.js 10.5.0
• Prior to Node.js 11.7.0, you could not access the
module unless you started node with the --
experimental-worker flag.
• isMainThread, parentPort, workerData,
resourceLimits, SHARE_ENV etc.. are the some of
the important setting for worker.
Worker Threads
10
WebSockets
• The WebSocket API is an advanced technology
that makes it possible to open a two-way interactive
communication session between the user's browser
and a server. With this API, you can send
messages to a server and receive event-driven
responses without having to poll the server for a
reply.
11
WebSockets in Nodejs
• Nodejs use ws npm module to support websockets
• ws is a simple to use, blazing fast, and thoroughly
tested WebSocket client and server
implementation.
12
WebSockets in Nodejs
13
Nodejs Applications of Websockets
• Chat apps
• Live location tracking on a map
• Live audience interaction
• Online auctions bid submission
• IoT device updates
14
What is Buffer
• Temporary storage for a chunk of data that is being
transferred from one place to another.
• The buffer is filled with data ,then passed along.
• Transfer small chunks of data at a time
15
Buffer
16
Stream is a collection of data – like an arrays, most
common difference is that , it might not available all at
once. Normally video or another large files do not fit to
memory. So backend application will read file as small
chunk for data and send it to bowser or another place.
17
Streams
18
Streams in Nodejs
• A stream is an abstract interface for working with
streaming data in Nodejs. The stream module
provides an API for implementing the stream
interface. It is core module of Nodejs so there is no
need to do npm install for this package.
19
Type of streams
1.Readable Streams
2.Writable Streams
3.Duplex Streams
4.Transform Streams
20
Readable Streams
21
• A readable stream, as the name suggests, allows
users to read data.
• They come in two variants or two different reading
modes, Paused and Flowing.
• There are different events like
data,end,error,readable,resume,pause
Another EventEmitter, writable streams, allow users to
write to a chosen destination.
22
Writable Streams
Transform Streams
It receives data from a input streams and manipulates
the data and transform the data .
23
Pipes and streams chaining
24
• Pipes are used to connect a streams to other.
Benefits of Streams
25
1. Time Efficiency
2. Spatial Efficiency
Cluster Module in Nodejs
• Cluster is group of node instances.
• There is one master node instances and other
worker instances.
• Take advantage of multi-core system
• Can communicate with the parent using IPC
26
Cluster Module in Nodejs
27
Connection with Database
• Connect with mysql
• Connect with psql
28
Questions?

More Related Content

Node js Chapter-2

  • 2. Agenda • Authentication and Authorization • Worker Threads • Web Sockets • Streaming • Cluster mode • Database connection 2
  • 3. Authentication Authentication is the act of validating that users are whom they claim to be. There are below ways to do authentication • Username/Passwords • Usernames and passwords are the most common authentication factor. If a user enters the correct data, the system assumes the identity is valid and grants access. • One-time pins • Grant access for only one session or transaction. • Authentication apps • Generate security codes via an outside party that grants access • Biometrics • A user presents a fingerprint or eye scan to gain access to the system.
  • 4. • We will discuss about Json Web Tokens for authentication. • JSON Web Tokens (JWT) have been introduced as a method of communicating between two parties securely. • JWT is very popular for handling authentication via HTTP. • It keeps user identify within the token and transfer token with http request. Authentication
  • 5. Authentication Client Server Client Server Login Request Signed JWT Data request + Signed JWT Response
  • 6. Authentication There are three section of JWT token as mentioned below, • Header – First section is header, which is base 64 encoded string • Data(Payload) – It contains JSON object send back to User. It is recommended not to include any sensitive data in JWTs, such as password. • Signature of Token - This is generated by hashing the string base64UrlEncode(header) + "." + base64UrlEncode(Data) + secret using the algorithm that is mentioned in the header section.
  • 7. Authorization • Authorization is a process the process of grating/denying access to resource which allows the user access various resources based on the user’s identity. • In Nodejs application, there could be different functionalities/APIs those can be accessed by different type of users depending on the role and granting/denying access as per the role is the authorization. • There are different ways to implement authorization. E.g. role based, oauth2., however we will discuss here role based authorization.
  • 8. 8 • The worker_threads module enables the use of threads that execute JavaScript in parallel. • Workers (threads) are useful for performing CPU- intensive JavaScript operations. • worker_threads can share memory. • They will not help much with I/O-intensive work. Node.js’s built-in asynchronous I/O operations are more efficient than Workers can be. Worker Threads
  • 9. Worker Threads 9 • worker_threads became available in Node.js 10.5.0 • Prior to Node.js 11.7.0, you could not access the module unless you started node with the -- experimental-worker flag. • isMainThread, parentPort, workerData, resourceLimits, SHARE_ENV etc.. are the some of the important setting for worker.
  • 11. WebSockets • The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply. 11
  • 12. WebSockets in Nodejs • Nodejs use ws npm module to support websockets • ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server implementation. 12
  • 14. Nodejs Applications of Websockets • Chat apps • Live location tracking on a map • Live audience interaction • Online auctions bid submission • IoT device updates 14
  • 15. What is Buffer • Temporary storage for a chunk of data that is being transferred from one place to another. • The buffer is filled with data ,then passed along. • Transfer small chunks of data at a time 15
  • 17. Stream is a collection of data – like an arrays, most common difference is that , it might not available all at once. Normally video or another large files do not fit to memory. So backend application will read file as small chunk for data and send it to bowser or another place. 17 Streams
  • 18. 18
  • 19. Streams in Nodejs • A stream is an abstract interface for working with streaming data in Nodejs. The stream module provides an API for implementing the stream interface. It is core module of Nodejs so there is no need to do npm install for this package. 19
  • 20. Type of streams 1.Readable Streams 2.Writable Streams 3.Duplex Streams 4.Transform Streams 20
  • 21. Readable Streams 21 • A readable stream, as the name suggests, allows users to read data. • They come in two variants or two different reading modes, Paused and Flowing. • There are different events like data,end,error,readable,resume,pause
  • 22. Another EventEmitter, writable streams, allow users to write to a chosen destination. 22 Writable Streams
  • 23. Transform Streams It receives data from a input streams and manipulates the data and transform the data . 23
  • 24. Pipes and streams chaining 24 • Pipes are used to connect a streams to other.
  • 25. Benefits of Streams 25 1. Time Efficiency 2. Spatial Efficiency
  • 26. Cluster Module in Nodejs • Cluster is group of node instances. • There is one master node instances and other worker instances. • Take advantage of multi-core system • Can communicate with the parent using IPC 26
  • 27. Cluster Module in Nodejs 27
  • 28. Connection with Database • Connect with mysql • Connect with psql 28