A simple example to implement WebSocket server using Akka HTTP

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. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become 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 HTTP

Leave a Reply

%d bloggers like this: