Software Ramblings

Scala Java and Android

ScalaWebSocket

| Comments

ScalaWebSocket

On April 21st I pushed the ScalaWebSocket library to GitHub. But what is it? As the name suggests, it’s WebSockets for Scala.

There are already a few implementations of WebSocket for Java, there is also the Scala project called wCS but all of them support Javaish style of passing the anonymous implementations of some kind of Listener interface. I didn’t want that as I prefer the functional style of handlers.

ScalaWebSocket is a thin wrapper around async-http-client and it exposes a more Scala-like vocabulary to interact with WebSockets.

Installation

To start using it in your project

Dependency in SBT
1
libraryDependncies += "eu.piotrbuda" %% "scalawebsocket" % "0.1.0"
Dependency in Maven
1
2
3
4
5
<dependency>
  <groupId>eu.piotrbuda</groupId>
  <artifactId>scalawebsocket_2.10</artifactId>
  <version>0.1.0</version>
</dependency>

Examples

Usage of this library is very simple.

Open a WebSocket connection
1
WebSocket().open("ws://echo.websocket.org/").sendText("text").close().shutdown()
Listen for text messages
1
2
WebSocket().open("ws://echo.websocket.org/")
.onTextMessage(msg => doSomethingWithMessage(msg))
Add several listeners
1
2
3
WebSocket().open("ws://echo.websocket.org/")
.onTextMessage(msg => doSomethingWithMessage(msg))
.onBinaryMessage(msg => doSomethingWithBinaryMessage(msg))

Future

I need this library to talk to SocketIO servers, so in version 0.2.0 I will implement some basic support for SocketIO. For now, please use it and report any issues you have. My goal for this library is to make WebSocket natural in Scala.

Comments