Fun with Scala and Vert.x

2

Vert.x is a polyglot event-driven application framework that runs on the Java Virtual Machine (JAVA 7 is the minimum supported version). Like Node.js, Vert.x is asynchronous and scalable, and lets developers build modern and effective web applications.

Being polyglot, Vert.x can be used in many flavors, among which : JavaScript,CoffeeScript, Ruby, Python,Groovy and Java. In order to enforce asynchronism and scalabity, Vert.x is built upon Netty, leverage the reactor pattern, using a frightening number of handlers.

This article aims at showing the powerful combination of Scala and Vert.x – the Java counterpart being provided as comparison.

Note : the source code are hosted on GitHub as part of lang-scala https://github.com/ouertani/vert.x/tree/master/vertx-lang/vertx-lang-scala

Vert.x supports many components :

  • WebSocket
  • HttpServer
  • Distributed Event Bus
  • TCP Server, SockJS ,… not presented here

I – WebSocket

Web Socket are HTML 5 feature providing full-duplex communications. For old browsers that do not support WebSocket, Vert.x provides SockJS as out-of-the-box component.

In order to run the following example check out : https://github.com/ouertani/vert.x/blob/master/vertx-examples/src/main/javascript/websockets/ws.html and save it into the compiled lib directory

Java version

scala version

II-HttpWebServer

Vert.x allows you to easily write full featured, highly performant and scalable HTTP and HTTPS servers.

The following example starts up an Http server, listening on port 8080, and logging all received requests.

Java

Scala

III-HttpClient

Vert.x also provides an HttpClient API, so as to interact with the server part. The following samples create and send a GET request, then log the server’s response.

Java

Scala

IV-EventBus

The event bus is like a vertebral spine, it can be used to connect distributed nodes, and to support interaction between different Verticles, even written in different languages.

Java

Scala

Conclusion

This article introduced the basic of Vert.x using scala language and short examples. Full Scala language support will soon, hopefully, become available.

Stay tuned, a subsequent post will show you more about Vert.x with Scala.

Partagez cet article.

A propos de l'auteur

2 commentaires

  1. Hi, how did you get the scala vertx to package into a jar?

    I have the vertx project but it doesn’t package the vertx-lang module.

Ajouter un commentaire