Search code examples
grailsatmosphere

Grails Atmosphere missed connection


I've used the sample to check how atmosphere works and a little modified it: add a service to send messages:

def sendMessage(String message){
        String mapping = "/jabber/chat/12345"
        Broadcaster b = BroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, mapping)
        println("Broadcast resources size:" +b.getAtmosphereResources().size())
        def resp = [type: "chat", resource: mapping, message: message] as JSON
        b.broadcast(resp)
    }

But looks like when I call the function some times AtmosphereResource for my connection in broadcaster is missed and client didn't recieve a message. Does anyone know what the problem is? Thanks for help.


Solution

  • After some debugging switched to SimpleBroadcaster implementation and now code works pretty fine:
    Handler class

    @Override
        void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            String mapping = "/jabber/chat" + request.getPathInfo()
            Broadcaster b = BroadcasterFactory.getDefault().lookup(SimpleBroadcaster.class, mapping, true)
            Meteor m = Meteor.build(request)
            m.setBroadcaster(b)
        }
    

    Service

    def sendMessage(String message){
            String mapping = "/jabber/chat/12345"
            Broadcaster b = BroadcasterFactory.getDefault().lookup(mapping)
            println("Broadcast resources size:" +b.getAtmosphereResources().size())
            def resp = [type: "chat", resource: mapping, message: message] as JSON
            b.broadcast(resp)
        }