Skip to content

JannikArndt/simple-akka-websocket-server-push

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server-Push Websocket with Akka HTTP

This is a minimal example for a websocket that pushes events to a client.

Start by running sbt:

$ sbt run
...
[info] Running Main 
Listening on 0:0:0:0:0:0:0:0:8080.

Now, start a websocket-client, for example wsc:

$ wsc ws://127.0.0.1:8080/ws
Connected to ws://127.0.0.1:8080/ws

You can now start typing in the (first) console and receive messages in the second one:

Input an empty line to quit.

The Code

The code consists of two objects:

  • Main creates the /ws route, binds it to WebSocket.listen(), starts the server and listens on StdIn for messages to send to Websocket.sendText().
  • WebSocket provides a listen function to register a new listener (or browserConnection) and a sendText function to send a message to every registered connection. The trick here is, that browserConnection stores a list of functions that can be called later. These functions are the offer functions on a Queue that sends them to the websocket.

About

Example for a simple server-push websocket with akka http

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages