





















































(For more resources related to this topic, see here.)
TCP servers are called net servers in Vert.x. Creating and using a net server is really similar to HTTP servers:
var vertx = require('vertx'); /* 1 */
var netServer = vertx.createNetServer(); /* 2 */
netServer.listen(1234); /* 3 */
Let's test whether this works. This time we need another terminal to run the telnet command:
$ telnet localhost 1234
The terminal should now be connected and waiting to send/receive characters. If you have "connection refused" errors, make sure the server is running.
Now, we need to place a block of code to be executed as soon as a client connects:
var vertx = require('vertx')
var server = vertx.createNetServer().connectHandler(
function(socket) {
// Composing a client address string
addr = socket.remoteAddress();
addr = addr.ipaddress + addr.port;
socket.write('Welcome to the chat ' + addr + '!');
}).listen(1234)
A NetServer connect handler accepts the socket object as a parameter; this object is our gateway to reading, writing, or closing the connection to a client.
We just learned how to execute a block of code at the moment in which the client connects. However now we are interested in doing something else at the time when we receive new data from a client connection.
The socket object we used in the previous step for writing data back to the client, accepts a handler function too: the data handler. Let's add one:
var vertx = require('vertx') var server = vertx.createNetServer().connectHandler( function(socket) { // Composing a client address string addr = socket.remoteAddress(); addr = addr.ipaddress + addr.port; socket.write('Welcome to the chat ' + addr + '!'); socket.dataHandler(function(data) { var now = new Date(); now = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds(); var msg = now + ' <' + addr + '> ' + data; socket.write(msg); }) }).listen(1234)
What we have now is a sort of an echo server, which returns back to the sender the same message with a prefix string.
The base requirement of a chat server is that every time a client sends a message, the rest of the connected clients should receive it. We will use event bus, the messaging service provided by the framework, to send (publish) received messages to a broadcast address. Each client will subscribe to the address upon connection and receive other clients' messages from there:
var vertx = require('vertx') var server = vertx.createNetServer().connectHandler( function(socket) { // Composing a client address string addr = socket.remoteAddress(); addr = addr.ipaddress + addr.port; socket.write('Welcome to the chat ' + addr + '!'); vertx.eventBus.registerHandler('broadcast_address', function(event){ socket.write(event); }); socket.dataHandler(function(data) { var now = new Date(); now = now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds(); var msg = now + ' <' + addr + '> ' + data; vertx.eventBus.publish('broadcast_address', msg); }) }).listen(1234)
$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Hello from terminal two!
13:6:56 <0:0:0:0:0:0:0:155991> Hello from terminal two!
13:7:24 <0:0:0:0:0:0:0:155992> Hi there, here's terminal three!
13:7:56 <0:0:0:0:0:0:0:155992> Great weather today!
Since Vert.x is a polyglot platform, we can choose to write an application (or a part of it) in either of the many supported languages. The granularity of the language choice is at verticle level. It's important to give a good architecture to a non-trivial project from the beginning.
Follow this list of generic principles to avoid performance bottlenecks or the need for massive refactoring in the future:
In this article, we learned in a step-wise procedure how we can create an Internet Relay Chat using the TCP server, and interconnect the server with the clients using an event bus, and enable different types of communication between them.
Further resources on this subject: