This simple example shows how to use the MessageService in order to send and receive messages to and from other JavaFX Clients. The user interface of the example consists of two parts: a sendWindow and a receiveWindow. The sendWindow contains a TextBox and a Button. When the user enters a text in the textbox and clicks on the "Send" button, the entered text is published on the channel with the name msgtest. The receiveWindow listens to all messages that are send in the msgtest channel and will display them.


In order to view this sample, you need to open 2 instances of the client application:
Start client 1
Start client 2


The code required for this sample is very straightforward and contains only 2 files:
MessageText.fx is the main script file,
MyMessageListener.fx is the JavaFX class that implements the listener that is notified when new messages are detected.


During initialization, a number of things need to be done:

  • First of all, the RedFX client needs to be told where the RedFX server is running.
  • A listener class needs to be created
  • The listener needs to be registered with the channel of interest.

65 var mml: MyMessageListener = MyMessageListener{};
67 public function run () {
68   MessageService.setServer ("http://localhost:8080/RedFXServer");

69   MessageService.subscribe("RedFXSampleMessaging_Client", "msgtest", mml);
70   stage;
71 }

Setting the location of the RedFX server is done by calling the setServer method on the org.redfx.client.messaging.MessageService class.

When the user clicks the "Send" button, the content of the textbox is send as a message on the channel named msgtest. This is done by calling the publish (channel: String, object: Object) on the org.redfx.client.messaging.MessageService class:

25     Button {
26       text: "Send"
27       action: function() {
28         MessageService.publish("msgtest", tb.text);
29       }
30     }