Dive into the fundamentals of WebSockets and discover how Akka brings a new dimension to real-time communication. This Session provides a beginner-friendly exploration of what WebSockets are and how Akka a powerful toolkit for building concurrent and distributed applications, enhances their functionality.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
▪ Punctuality
Join the session 5 minutes prior to the session start time. We start on
time and conclude on time!
▪ Feedback
Make sure to submit a constructive feedback for all sessions as it is very
helpful for the presenter.
▪ Silent Mode
Keep your mobile devices in silent mode, feel free to move out of session
in case you need to attend an urgent call.
▪ Avoid Disturbance
Avoid unwanted chit chat during the session.
3. Agenda
1. Introduction
o What is WebSocket
o WebSocket Communication
o Use cases of WebSocket
2. WebSocket vs HTTP
3. Overview of Akka Actor
4. Akka HTTP and Its Features
5. Overview of Akka Stream
6. Akka support for WebSocket
7. Demo
5. What is WebSocket.
▪ WebSocket function as a communication protocol, offering
full-duplex communication channels over a single, persistent
TCP (Transmission Control Protocol) connection.
▪ They designed this to overcome some limitations inherent in
traditional HTTP (Hypertext Transfer Protocol)
communication, which primarily relies on a request-response
model.
▪ WebSocket find frequent application in web-based
functionalities, facilitating real-time features like live chat,
notifications, collaborative editing, financial trading platforms,
and online gaming.
6. WebSocket Communication
▪ Handshake Initiation: WebSocket starts with a
handshake between the client and server,
utilizing the HTTP protocol, setting the
groundwork for the subsequent transition.
▪ HTTP Protocol Utilization: The handshake
relies on the HTTP protocol, ensuring
compatibility and smooth integration within
existing web infrastructures.
▪ Transition to WebSocket: After a successful
handshake, the connection seamlessly shifts
from HTTP to the WebSocket protocol.
▪ Bidirectional Communication: WebSocket
activation enables bidirectional communication
between the client and server.
7. What are WebSocket used for?
WebSocket enable real-time bidirectional communication between a client (typically a web
browser) and a server through a single, persistent TCP connection. This technology proves
especially effective in scenarios where low-latency, high-throughput, and real-time updates are
essential.
Consequently, here are some common use cases for WebSocket:
Real-Time Web Applications:
▪ WebSocket find extensive use in developing real-time web applications, including chat
applications, collaborative document editing and live streaming. The bidirectional nature of
WebSocket allows instant communication between clients and servers.
Online Gaming:
▪ Online gaming frequently relies on WebSocket to enable real-time communication between
players and the game server. This is crucial for multiplayer games where quick responses and
synchronization are essential for a seamless gaming experience.
8. What are WebSocket used for?
Notifications and Alerts:
▪ WebSocket serve to deliver instant notifications and alerts to users. This can include push
notifications for updates, system alerts, or any event-triggered communication that requires
immediate attention.
IOT(Internet of Things):
▪ In IoT applications, WebSocket serve for bidirectional communication between IoT devices and
the server. This allows for real-time monitoring, control, and updates in scenarios like smart
homes, industrial automation, and connected vehicles.
9. WebSocket vs HTTP.
▪ Encryption: WebSocket can be used over
a secure connection (WSS - WebSocket Secure)
to encrypt the data, providing a secure
channel similar to HTTP.
▪ Real-Time Applications: WebSocket
are ideal for real-time applications where low
latency and bidirectional communication are
essential, such as chat applications, online
gaming, and live updates.
▪ Long-Lived Connections: WebSocket keep
the connection open for an extended
period, allowing for efficient communication
without the need to repeatedly establish
connections.
WebSocket
▪ Encryption: HTTP encrypts the
communication between the client and the
server, providing a secure and private channel. It
uses TLS (Transport Layer Security) or its
predecessor, SSL (Secure Sockets Layer), to
encrypt the data.
▪ Web Pages: HTTP is used for
loading secure web pages, where sensitive
information such as login credentials or personal
data is transmitted.
▪ Short-Lived Connections: In HTTP,
each interaction typically involves a short-
lived connection. The client establishes a
connection for a request, and the connection is
closed after the response is received.
HTTP
10. WebSocket vs HTTP.
WebSocket:
• Full-Duplex: WebSocket provide full-
duplex communication, allowing both the
client and server to send messages
independently over the same, long-lived connection.
• Bidirectional: Either the client or the
server can initiate communication at any time.
HTTP:
• Request-Response: HTTP is
primarily based on the request-response
model. A client sends a request to a server,
and the server responds with the requested
data.
• Stateless: Each request from the
client is independent, and the server does
not maintain any state between requests.
12. Akka Actor.
▪ An actor is an entity which communicates to other actor by message passing. Actor has it's
own state and behaviour.
▪ As in object-oriented programming everything is an object same like everything is an actor in
actor-based system. In other words, we can say that an actor is an object that encapsulates
state and behaviour.
▪ Actors provide a higher level of abstraction to write concurrent and distributed programs.
Roughly speaking, an Actor is a unit of concurrently manageable work and its associated state.
▪ An Actor’s state is strictly private to an actor. Only way to communicate with an Actor is by
sending messages to it.
13. Actor Model.
Mailboxes
▪ Actors have mailboxes attached to them. Mailboxes are nothing but a queue data structure,
which can be concurrently accessed. Messages sent to an actor are queued in mailboxes.
▪ Messages in a mailbox are dispatched to an Actor one at a time in order.
Dispatchers
▪ Dispatchers the execution engine for Actors. Dispatchers internally manage a thread pool on
which Actors are scheduled and execute the message handling code.
15. Akka HTTP: Introduction
▪ Akka HTTP is a powerful, open-source toolkit for building scalable, high-performance web
applications in Scala. Developed on top of the Akka toolkit, it brings the actor-based
concurrency model into the world of web development.
▪ Akka HTTP simplifies the integration of WebSocket communication into our applications.
It offers a high-level API for handling WebSocket connections, allowing developers to focus
on business logic rather than dealing with low-level details.
▪ With Akka HTTP, you can easily establish WebSocket connections, handle incoming
messages, and broadcast updates to connected clients.
16. Key Features
▪ Actor-Based Concurrency: Akka HTTP leverages the Actor model, providing a scalable and
efficient way to handle concurrent requests. Each HTTP request is processed by lightweight,
isolated actors, ensuring optimal resource utilization.
▪ Streaming Capabilities: With Akka HTTP, streaming is a first-class citizen. It supports both
client and server streaming, making it ideal for scenarios where handling large amounts of data
in real-time is crucial.
▪ RESTful Routing: Akka HTTP simplifies the process of defining RESTful APIs through a
concise and expressive routing DSL (Domain Specific Language). This allows developers to
map HTTP requests to specific actions with ease.
17. Key Features
▪ Comprehensive WebSocket Support: Akka HTTP provides seamless integration for
WebSocket communication. Its high-level API simplifies the establishment of WebSocket
connections, allowing bidirectional communication for real-time applications.
▪ Actor-Based Concurrency Model: Akka HTTP leverages the Actor model, a paradigm
for concurrent and distributed computing, to manage WebSocket connections efficiently. Actors
in Akka provide a lightweight, isolated concurrency unit, making it easier to handle
multiple WebSocket connections concurrently without compromising performance or
introducing complex synchronization mechanisms.
19. Akka Streams
▪ Akka-Stream is a module built on top of Akka Actors. It means there are underlying actors that
are working in transferring data.
▪ It is a library to process and transfer a sequence of elements.
▪ Akka Streams is nonblocking that means a certain operation does not hinder the progress of
the calling thread.
▪ It provides easy-to-use APIs to create streams that leverage the power of the Akka toolkit
without explicitly defining actor behaviors and messages. That means, a relief from handling
actors yourself.
▪ We can focus on logic rather than focusing on how to manage actors and that can be much
productive.
20. Basic Terminology in Akka-Streams
Akka streams consist of 3 major components in it – Source, Flow, Sink – and any non-cyclical
stream consist of at least 2 components Source, Sink and any number of Flow element. Here we
can say Source and Sink are the special cases of Flow.
▪ Source – this is the Source of data. It has exactly one output. We can think of Source as
Publisher.
▪ Sink – this is the Receiver of data. It has exactly one input. We can think of Sink as Receiver.
▪ Flow – this is the Transformation that acts on the Source. It has exactly one input and one
output.
Here Flow sits in between the Source and Sink as they are the Transformations applied on the
Source data.
21. Akka HTTP Support for WebSocket.
▪ Akka HTTP boasts robust support for WebSocket
communication, providing developers with a powerful toolkit
for implementing real-time, bidirectional data exchange in
their Scala-based web applications.
▪ Leveraging the underlying actor-based concurrency model,
Akka HTTP simplifies WebSocket integration, allowing for
the creation of efficient and scalable solutions.
▪ With dedicated APIs for handling WebSocket connections,
developers can seamlessly implement features such as
message processing and connection management.
▪ Akka HTTP's support for WebSocket enhances the toolkit's
versatility, making it a compelling choice for building
responsive and dynamic web applications that require
instant updates and interactive user experiences.