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.

Advertisements

About Ayush Mishra

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. .
This entry was posted in Akka, Scala, Web Services and tagged , , , , . Bookmark the permalink.

2 Responses to A simple example to implement WebSocket server using Akka HTTP

  1. Pingback: A Simple Example of WebSocket based Microservice Architecure using Akka-HTTP in Scala | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s