Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

BroadcastingMessagesToOtherUsers.asciidoc 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ---
  2. title: Broadcasting Messages To Other Users
  3. order: 82
  4. layout: page
  5. ---
  6. [[broadcasting-messages-to-other-users]]
  7. Broadcasting messages to other users
  8. ------------------------------------
  9. In this tutorial we will create an application where any user can send a
  10. broadcast message to all other active users. We will start from a
  11. project where push has been enabled (see link:EnablingServerPush.asciidoc[Enabling
  12. server push] for details).
  13. For simplicity, we will use a static `Broadcaster` which is shared between
  14. all users and all sessions. Each UI will register itself to this
  15. broadcast when initialized and unregister when detached. The broadcaster
  16. will take care of sending events to the registered UI:s as needed. In a
  17. real world scenario you probably want to use something else than a
  18. shared static class (e.g. JMS or some other messaging system) but the
  19. same ideas apply.
  20. So, let’s start from a simple `Broadcaster` class and a listener
  21. interface. We need the possibility to register and unregister listeners
  22. and to broadcast a message to the listeners so we end up with the
  23. following class:
  24. [source,java]
  25. ....
  26. public class Broadcaster {
  27. private static final List<BroadcastListener> listeners = new CopyOnWriteArrayList<BroadcastListener>();
  28. public static void register(BroadcastListener listener) {
  29. listeners.add(listener);
  30. }
  31. public static void unregister(BroadcastListener listener) {
  32. listeners.remove(listener);
  33. }
  34. public static void broadcast(final String message) {
  35. for (BroadcastListener listener : listeners) {
  36. listener.receiveBroadcast(message);
  37. }
  38. }
  39. public interface BroadcastListener {
  40. public void receiveBroadcast(String message);
  41. }
  42. }
  43. ....
  44. As Broadcast will be used by many threads simultaneously, we need to
  45. ensure that it is thread-safe. We will do it here by using the
  46. thread-safe `CopyOnWriteArrayList` class for keeping track of the
  47. listeners.
  48. Now that we have the `Broadcaster` implemented we can use it in our UI for
  49. instance as follows:
  50. [source,java]
  51. ....
  52. @Push
  53. public class BroadcasterUI extends UI implements BroadcastListener {
  54. @Override
  55. protected void init(VaadinRequest request) {
  56. [...]
  57. // Register broadcast listener
  58. Broadcaster.register(this);
  59. }
  60. @Override
  61. public void detach() {
  62. Broadcaster.unregister(this);
  63. super.detach();
  64. }
  65. @Override
  66. public void receiveBroadcast(final String message) {
  67. access(new Runnable() {
  68. @Override
  69. public void run() {
  70. Notification n = new Notification("Message received",
  71. message, Type.TRAY_NOTIFICATION);
  72. n.show(getPage());
  73. }
  74. });
  75. }
  76. ....
  77. We register the UI in the init method and unregister it in the detach
  78. method to avoid receiving messages for UIs no longer in use (and
  79. ensuring that the detached UI can be garbage collected).
  80. When we receive a broadcast message we need to use the access method as
  81. this call comes from a thread where the UI is not locked.
  82. `access(Runnable)` will take care of locking the UI for us so we can
  83. update it. In the wrapped run method we can do whatever we like with the
  84. received message, for instance show it as a tray notification as done
  85. here.
  86. To send a broadcast message we can create a simple user interface in our
  87. UI init method:
  88. [source,java]
  89. ....
  90. protected void init(VaadinRequest request) {
  91. final VerticalLayout layout = new VerticalLayout();
  92. layout.setMargin(true);
  93. setContent(layout);
  94. final TextArea message = new TextArea("",
  95. "The system is going down for maintenance in 10 minutes");
  96. layout.addComponent(message);
  97. final Button button = new Button("Broadcast");
  98. layout.addComponent(button);
  99. button.addClickListener(new Button.ClickListener() {
  100. @Override
  101. public void buttonClick(ClickEvent event) {
  102. Broadcaster.broadcast(message.getValue());
  103. }
  104. });
  105. // Register broadcast listener
  106. Broadcaster.register(this);
  107. }
  108. ....
  109. Now if you deploy the application and open it in a couple of browser
  110. tabs or separate browsers you are able to send messages between the
  111. instances.