Review Matters Admin
Review Matters Admin

Reputation: 73

websocket connection not happening using grails event-push plugin

I am trying to integrate grails events-push plugin to push events to browser however its not working. I made below changes for it

BuildConfig.groovy

 grails.tomcat.nio = true
 compile ":events-push:1.0.M7"

Config.groovy

events.push.servlet.initParams = [
        'org.atmosphere.cpr.cometSupport': 'org.atmosphere.container.Tomcat7CometSupport',
        "org.atmosphere.cpr.CometSupport.maxInactiveActivity": "100000"
 ]

  tomcat.nio=true

Deleted context.xml(generated by plugin) from META-INF folder as tomcat was not working with it

In Client side i.e angular js

window.grailsEvents = new grails.Events('http://localhost:8080');

I managed to start my application successfully. I also below message in log

DEBUG cpr.DefaultBroadcaster  - Broadcaster eventsbus doesn't have any associated resource. Message will be cached in the configured BroadcasterCache

Bu when I open my application in browser websocket do not work. In serve end I see below meesage

2014-05-01 15:19:56,365 [http-nio-8080-exec-3] DEBUG cpr.AsynchronousProcessor  - Timing out the connection for request AtmosphereRequest{ contextPath= servletPath=/g-eventsbus pathInfo=/eventsbus requestURI=/g-eventsbus/eventsbus requestURL=http://localhost:8080/g-eventsbus/eventsbus destroyable=false}
2014-05-01 15:19:56,366 [http-nio-8080-exec-3] WARN  websocket.DefaultWebSocketProcessor  - Unable to retrieve AtmosphereResource for org.apache.catalina.websocket.WsOutbound@269dd750
2014-05-01 15:19:57,783 [http-nio-8080-exec-5] DEBUG cpr.AsynchronousProcessor  - Timing out the connection for request AtmosphereRequest{ contextPath= servletPath=/g-eventsbus pathInfo=/eventsbus requestURI=/g-eventsbus/eventsbus requestURL=http://localhost:8080/g-eventsbus/eventsbus destroyable=false}

I browser console end I see

WebSocket connection to 'ws://localhost:8080/g-eventsbus/eventsbus?X-Atmosphere-tracking-id=0&X-Atmosphere-Framework=1.1.0.beta3&X-Atmosphere-Transport=websocket&X-Atmosphere-TrackMessageSize=true&X-Cache-Date=0&topics=eventsbus' failed: WebSocket is closed before the connection is established. 

Guys please help me I am struggling with this plugin from long time.

Upvotes: 1

Views: 1085

Answers (2)

mpccolorado
mpccolorado

Reputation: 827

I'm using grails-events-push and almost everything works well. In BuildConfig:

grails.servlet.version = "3.0"
grails.tomcat.nio=true
...
dependencies {
    ...
    compile 'org.grails.plugins:events:1.0.0.BUILD-SNAPSHOT'
    compile 'org.atmosphere:atmosphere-runtime:2.1.4'
}

plugins {
...
    build ":tomcat:7.0.52.1"
    runtime ":events-push:1.0.0.BUILD-SNAPSHOT"
}

You have to create one file to declare your events: mine is EasyRestaurantEvents.groovy

import static reactor.event.selector.Selectors.*

includes = ['push']

doWithReactor = {
    reactor('grailsReactor'){
        ext 'browser', [
            (R('oneMessage-([0-9]+)')) : true
        ]
    }

    reactor('browser'){
        ext 'browser', [
            'oneMessageFromBrowser' : true
        ]
    }
}

In the controller or service I can send an event in this way:

event('oneMessage-' + someId, mapObject)

In the client app I can receive this message in this way:

grailsEvents.on("oneMessage-666",
     function(event){
         alert("oneMessage was received for client 666");
     });

In the server app, I can receive a message from the browser, in this way:

import reactor.spring.annotation.ReplyTo
import reactor.spring.annotation.Selector

class OneService {

    @Selector(reactor = 'browser')
    @ReplyTo
    def oneMessageFromBrowser(Map data){
        //do some work
    }
}

To send an event from the browser yo can do:

grailsEvents.send('oneMessageFromBrowser', {message:'hello from browser'});

I hope this helps! I struggled with this plugin a lot! =( But is very easy to use (when you make it work)

PS: I used another application created in angular to communicate with the server so I have to import the js manually:

"atmosphere.js": 2.1.5-javascript
"jquery.atmosphere.js": 2.1.5-jquery

Upvotes: 2

Review Matters Admin
Review Matters Admin

Reputation: 73

Thanks mpccolorado for you reply. I got it working actually issue was in JS grails.Events should be created with globalTopicName.

var grailsEvents = new grails.Events(GRAILS_EVENT_URL, {globalTopicName: 'newReview'});

Upvotes: 1

Related Questions