Broadcasting messages to other users

In this tutorial we will create an application where any user can send a broadcast message to all other active users. We will start from a project where push has been enabled (see Enabling server push for details).

For simplicity, we will use a static Broadcaster which is shared between all users and all sessions. Each UI will register itself to this broadcast when initialized and unregister when detached. The broadcaster will take care of sending events to the registered UI:s as needed. In a real world scenario you probably want to use something else than a shared static class (e.g. JMS or some other messaging system) but the same ideas apply.

So, let’s start from a simple Broadcaster class and a listener interface. We need the possibility to register and unregister listeners and to broadcast a message to the listeners so we end up with the following class:

Java

  1. public class Broadcaster {
  2. private static final List<BroadcastListener> listeners = new CopyOnWriteArrayList<BroadcastListener>();
  3. public static void register(BroadcastListener listener) {
  4. listeners.add(listener);
  5. }
  6. public static void unregister(BroadcastListener listener) {
  7. listeners.remove(listener);
  8. }
  9. public static void broadcast(final String message) {
  10. for (BroadcastListener listener : listeners) {
  11. listener.receiveBroadcast(message);
  12. }
  13. }
  14. public interface BroadcastListener {
  15. public void receiveBroadcast(String message);
  16. }
  17. }

As Broadcast will be used by many threads simultaneously, we need to ensure that it is thread-safe. We will do it here by using the thread-safe CopyOnWriteArrayList class for keeping track of the listeners.

Now that we have the Broadcaster implemented we can use it in our UI for instance as follows:

Java

  1. @Push
  2. public class BroadcasterUI extends UI implements BroadcastListener {
  3. @Override
  4. protected void init(VaadinRequest request) {
  5. [...]
  6. // Register broadcast listener
  7. Broadcaster.register(this);
  8. }
  9. @Override
  10. public void detach() {
  11. Broadcaster.unregister(this);
  12. super.detach();
  13. }
  14. @Override
  15. public void receiveBroadcast(final String message) {
  16. access(new Runnable() {
  17. @Override
  18. public void run() {
  19. Notification n = new Notification("Message received",
  20. message, Type.TRAY_NOTIFICATION);
  21. n.show(getPage());
  22. }
  23. });
  24. }

We register the UI in the init method and unregister it in the detach method to avoid receiving messages for UIs no longer in use (and ensuring that the detached UI can be garbage collected).

When we receive a broadcast message we need to use the access method as this call comes from a thread where the UI is not locked. access(Runnable) will take care of locking the UI for us so we can update it. In the wrapped run method we can do whatever we like with the received message, for instance show it as a tray notification as done here.

To send a broadcast message we can create a simple user interface in our UI init method:

Java

  1. protected void init(VaadinRequest request) {
  2. final VerticalLayout layout = new VerticalLayout();
  3. layout.setMargin(true);
  4. setContent(layout);
  5. final TextArea message = new TextArea("",
  6. "The system is going down for maintenance in 10 minutes");
  7. layout.addComponent(message);
  8. final Button button = new Button("Broadcast");
  9. layout.addComponent(button);
  10. button.addClickListener(new Button.ClickListener() {
  11. @Override
  12. public void buttonClick(ClickEvent event) {
  13. Broadcaster.broadcast(message.getValue());
  14. }
  15. });
  16. // Register broadcast listener
  17. Broadcaster.register(this);
  18. }

Now if you deploy the application and open it in a couple of browser tabs or separate browsers you are able to send messages between the instances.