Our Chat example contains a client project and a server project. The client project is a JavaFX application using RedFX Client to communicate with the server project.
When a user enters a message, the message text is sent to the server and the server will echo the message back to all clients that are currently connected and interested in that message. The client will show this message in a ListView

Server project

The server part of this sample is extremely basic and involves no project-specific code. The RedFX Server jars are built into the project, and a web.xml configuration file instructs the application server to accept REST calls on a specified endpoint. Furthermore, a beans.xml configuration file is required to enable CDI (which is used by RedFX Server). The server project can be downloaded here as a Netbeans project.
The code has been deployed succesfully on Glassfish 3.1.2. The only thing you have to do in the Glassfish Admin Console is to enable Comet in the http-listener.


The Client project contains 2 source files. The code can be downloaded here as a Netbeans project. We'll now show how the client uses RedFX to send and receive chat messages. (line numbers are included as a reference)
First of all, a RedFX Client instance has to be created:

42     private final String SERVER = "http://localhost:8080/DemoChatServer";
55     client = new RedFXClient(CometClientController.class, SERVER);

The SERVER variable contains the location of the server project. The configuration files in our server project will cause it to be available on the DemoChatServer context.
RedFX currently supports two communication modes:

1. Comet
2. WebSocket

The RedFXClient constructor takes the communication mode and the server location as parameters.
We have to create a ChatReader, and register it with RedFX. This is required for receiving messages.

54     reader = new ChatReader(chatItems);
56     client.register(reader, null);

The messages we receives will be shown in a ListView, and the items list of the ListView is passed into the constructor of our ChatReader instance.
The ChatReader extends the RedFX RedReader class, and it has an annotated method that will be called by RedFX when a new message arrives on a Channel we are interested in:

35   @Channel("demo")
36   public void chat(final String message) {
42         items.add(message);
45   }

The @Channel annotation with the value "demo" specifies that this method will be called when the RedFX Server receives a message on the channel named "demo". When such a message arrives, we add it to the ObservableList items. Note that we have to execute this code on the JavaFX Application Thread, since the ListView will be updated. That part has been ommitted from the code snippet above.
Finally, we have to be able to send messages to the channel "demo". Our example contains a TextField and a Button, and clicking the button will send the value of the textfield to the RedFX Server:

69         client.publish(reader, "demo", textField.getText());

The second argument in the publish method contains the name of the Channel we want to send this message to.

This sample is too simple to be useful in production. The chat messages are plain Strings, but they can be any Objects (containing information about the author, the time,...). The server project can intercept messages, modify them, or generate other messages.
With this basic sample, we want to show how easy it is to develop the core of a Client-Server chat application with JavaFX and RedFX.