A simple example to implement WebSocket server using Akka HTTP

Table of contents
Reading Time: 2 minutes

This is a very simple example to implement WebSocket server using Akka HTTP. If we have the scenario to transfer real time data from and to server over a HTTP connection, we use WebSocket. Akka provides Akka HTTP library to interact with web services. Akka HTTP provides a stream-based implementation of the WebSocket protocol.

In this example, you will see how easily you can create web socket server using Akka HTTP. Akka HTTP provides support for low level and high level HTTP server APIs. I am using high level API in this example.

Add dependencies in build.sbt
11

Add  below lines in Boot.scala to start HTTP server
22

Now define routes

33

Here  handleWebSocketMessages  directive is used to install a WebSocket handler if the request was a WebSocket request

Download  Dark Websocket terminal web socket client extension of google chrome to test this example

You can find complete code here.

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

2 thoughts on “A simple example to implement WebSocket server using Akka HTTP1 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading