Hi, i’m using Vaadin 24 and i followed example from https://vaadin.com/docs/latest/advanced/server-push (last 3 code blocks).
Unfortunately it isn’t working for me. I add my code, but it’s almost the same as in the official example, I just replaced Consumer with BiConsumer, and I also use my custom MessageList and MessageListItem classes.
@Override
protected void onAttach(AttachEvent attachEvent) {
UI ui = attachEvent.getUI();
broadcasterRegistration = Broadcaster.register((newMessage, conversationId) -> {
ui.access(() -> messageList.addItems(List.of(newMessage)));
});
}
@Override
protected void onDetach(DetachEvent detachEvent) {
broadcasterRegistration.remove();
broadcasterRegistration = null;
}```
```java
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Broadcaster {
static Executor executor = Executors.newSingleThreadExecutor();
static LinkedList<BiConsumer<MessageListItem, String>> listeners = new LinkedList<>();
public static synchronized Registration register(BiConsumer<MessageListItem, String> listener) {
listeners.add(listener);
return () -> {
synchronized (Broadcaster.class) {
listeners.remove(listener);
}
};
}
public static synchronized void broadcast(MessageListItem item, String conversationId) {
for (BiConsumer<MessageListItem, String> listener : listeners) {
executor.execute(() -> listener.accept(item, conversationId));
}
}
}```
```java
@SpringBootApplication
@Push
public class CasualtalkchatApplication implements AppShellConfigurator {
public static void main(String[] args) {
SpringApplication.run(CasualtalkchatApplication.class, args);
}
}```