Pushing data from one user to another


Hi!

I get the fact that it might take more than 10 lines of code (hopefully not more than 50), but I was wondering if you could help me anyway.
I’m trying to update one user’s UI thread at runtime, based on another user’s input. I’ve created a basic project which implements three predefined users (jim, tom and threeskin). I’d like to send a message from jim to tom and have it appear as a new Label object in tom’s UI, without threeskin ever knowing about it, even though they’are all logged in. Oh, and jim shouldn’t have to refresh his page. The label should just spawn on screen out of it’s own accord. To say that I’d appreciate some help would be the understatement of the decade. Vaadin 7 project in Eclipse running on local Tomcat8.0. Thanks a bunch!

    public class User {
    public String nume;

    public User(String nume) {
        super();
        this.nume = nume;
      }
    }
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    public class Engine implements ServletContextListener {
    public static ArrayList<User>userbase;
        public void contextDestroyed(ServletContextEvent arg0) { }
        public void contextInitialized(ServletContextEvent arg0) {
            System.out.println("This code is running at startup");
            userbase =new ArrayList<User>();
            userbase.add(new User("jim"));userbase.add(new User("tom"));userbase.add(new User("threeskin"));        
        }
    }
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    public class InfigeUI extends UI {    
        User us3r;
        @WebServlet(value = "/*", asyncSupported = true)
        @VaadinServletConfiguration(productionMode = false, ui = InfigeUI.class)    
        public static class Servlet extends VaadinServlet {}    
            protected void init(VaadinRequest request) {
            VerticalLayout everything=new VerticalLayout();
            setContent(everything);
        if (us3r==null){everything.addComponent(auth());}else{everything.addComponent(main());}    
        }
        ComponentContainer auth(){    
            final VerticalLayout layout = new VerticalLayout();        
            layout.setMargin(true);
            TextField userField=new TextField();
            Button login = new Button("Log in");
            login.addClickListener(new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {                
                    us3r=login(userField.getValue());
                    if (us3r!=null){
                    saveValue(InfigeUI.this, us3r);    
                    layout.removeAllComponents();
                   layout.addComponent(main());
                }else{Notification.show("I only know jim, tom and threeskin. Which one are you?");}}
            });
            layout.addComponent(userField);
            layout.addComponent(login);
            return layout;
        }    
        User login(String nume){
            for (int i=0;i<Engine.userbase.size();i++){
                if (nume.equals(Engine.userbase.get(i).nume)){return Engine.userbase.get(i);}                                
            }
            return null;
        }
    static void saveValue(InfigeUI ui,User value){
        ui.us3r=value;
        ui.getSession().setAttribute("something", value);
         VaadinService.getCurrentRequest().getWrappedSession().setAttribute("something", value);            
    }
    ComponentContainer main(){
        VerticalLayout vl=new VerticalLayout();
        Label label=new Label("This is the post-login screen");
        String name=new String(us3r.nume);
        Label eticheta=new Label(name);
        TextField to=new TextField("Send to");
        TextField message=new TextField("Message");
        Button sendNow=new Button("Send now!");
       vl.addComponent(eticheta);
        vl.addComponent(label);
        vl.addComponent(eticheta);
        vl.addComponent(to);
        vl.addComponent(message);
        vl.addComponent(sendNow);
        return vl ;
        }
    }

I would point you to
https://vaadin.com/book/-/page/advanced.push.html
and there especially to “11.16.4 Broadcasting to Other Users”, even though you will also need the push part.