Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send message to all connected web socket clients #645

Closed
mer23 opened this issue Feb 13, 2017 · 2 comments
Closed

Send message to all connected web socket clients #645

mer23 opened this issue Feb 13, 2017 · 2 comments
Milestone

Comments

@mer23
Copy link

mer23 commented Feb 13, 2017

I'm using Jooby's MVC routes for an API. I have also set up a websocket, to which a few clients connect. What I'm trying to do is send a message to all connected websocket clients whenever a specific http request is received in the server. This is how my route method looks like:

    @Path("/player")
    @Produces("application/json")
    public class PlayerRoute {
        @POST
        public Result newPlayer(Request req, @Body Player player) {
            //this is what I'm trying to achieve..
            allWebsocketSessions.foreach(session ->
                    session.send("a new player has been created")
            );
            return Results.ok();
        }
}

I've read jooby's documentation but can't figure out how to do it.

Thanks in advance.

@jknack
Copy link
Member

jknack commented Feb 13, 2017

Correct, there is no way to do such thing today. But what you showed me should works.

Go to your application do something like:

public static final List<WebSocket> allWebsocketSessions = new CopyOnWriteArrayList<>();

{
    ws("/ws", ws -> {
       sessions.add(ws);

       ws.onClose(() -> sessions.remove(ws));
    }); 
}

From your controller you can access to allWebsocketSessions.

It is a (not that ugly) workaround.

Let's keep this open and will find a way to do it automatically.

@jknack jknack added this to the 1.1.0 milestone Feb 13, 2017
@mer23
Copy link
Author

mer23 commented Feb 14, 2017

Worked perfectly! Thanks!

@mer23 mer23 closed this as completed Feb 14, 2017
@mer23 mer23 reopened this Feb 14, 2017
@jknack jknack closed this as completed in 60628f9 Mar 21, 2017
jknack added a commit that referenced this issue Mar 21, 2017
Send message to all connected web socket clients fix #645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants