Use grails-events-push in grails to implement ajax/Comet/websocket message push

Source: Internet
Author: User
Tags grails
> The push technology on the browser side is very mature. The grails Events push plug-in is used here.
>
> Events push is an extension of grails-platform-Core Based on http://grailsrocks.github.com/grails-platform-core/guide/ and https://github.com/Atmosphere/atmosphere.
> Here we use the Events push example and make some addendum to it.

# Create a grails project first.

# Install the plug-in events-push. Version 1.0.m7 is used here.

# Modify buildconfig. grooy
Add the atmosphere-runtime MVN repository to repositories

Repositories {
...
Mavenrepo http://mvn.atns.de/repository/internal"
...
}

### Add in plugins

Plugins {
...
Complie "'org. Atmosphere: Atmosphere-runtime: 1.1.0-Snapshot '"
Compile ": events-push: 1.0.m7"
...
}

### The events-push.1.0.M7 references atmosphere-runtime: 1.1.0-snapshot.

# Create myevents. Groovy In the config directory

Events = {
// The browser must receive the notification "msgfromserver ".
'Msgfromserver' namespace: 'browser ', browser: True, browserfilter: {data, request->
Println "browserfilter after service"
Println "browserfilter data: $ data"
Return false
}

// The browser sends the notification named msgtoserver.
'Msgtoserver' namespace: 'browser ', browser: True, filter: {println "msgtoserver filter"; return true}
}

### In the above Code
##### 'Msgtoserver' message label and message name
##### Namespace message group, group name
##### Whether the Browser allows the browser to accept the message. The grails-platform-core plug-in is mainly used for pushing events on the server. Events push integrates it with atmosphere, so we have this parameter.
##### Filter access filter closure, equivalent to xxfilters. Groovy in config, which usually performs some permission checks or something. After using atmosphere, Ajax/comet access will not go through xxxfilters. groovy, you need to make a judgment here. Of course, just write the closure like filters. The return value determines whether to intercept messages from the browser to the server.
##### After the service processes the messages pushed by the browser, browserfilter will execute the following command to accept the message content sent by the event () method in the data: Service parameter, request: Servlet request. Like filters, the return value determines whether to intercept messages from the server to the browser.

# Create a service named myservice. Groovy

// The Listener annotation specifies the message that this method accepts. If namespace is not specified, all messages are accepted. Here, messages named browser group are accepted.
// Note that the method name must be the Tag Name Defined in myevents. Groovy
@ Listener (namespace = 'browser ') def msgtoserver (Map Data ){
Println "MSG from browser MSG: $ data"
Println "service send 'msgfromserver' MSG"
// Send the "msgfromserver" Event
Event ('msgfromserver', [MSG :"! This is MSG! "], [Namespace: 'browser '])
}

# Modify a GSP by yourself and add the event push listener and message sender

HTML:

### The transport parameter accepted in grails. events is supported by atmosphere and basically contains various ajax/Comet/websocket protocols.
### Supported polling, long-polling, streaming, jsonp, SSE, and websocket

# Output result of println for the entire processing process:

##### 1. msgtoserver filter> filter of myevents. Groovy msgtoserver tag
##### 2. MSG from browser MSG: [MSG: MSG from browser]> myservice. Groovy msgtoserver Method
##### 3. service send 'msgfromserver' MSG> myservice. Groovy msgtoserver Method
##### 4. browserfilter after service> browserfilter of myevents. Groovy msgtoserver tag
##### 5. browserfilter data: [MSG :! This is MSG!] > Myevents. Groovy msgtoserver tag browserfilter Filter

***
# Reference:
##### Server Response push technology such as ajax/comet, IBM article series http://www.ibm.com/developerworks/cn/views/web/libraryview.jsp? View_by = SEARCH & sort_by = Date & sort_order = DESC & view_by = SEARCH & search_by = % E5 % 8f % 8d % E5 % 90% 91 + Ajax + % E9 % 83% A8 % E5 % 88% 86 & dwsearch. X = 14 & dwsearch. y = 15
##### Using browser pushes in grails http://java.dzone.com/articles/using-browser-push-grails
##### Grails-Atmosphere-plugin framework https://bitbucket.org/bgoetzmann/grails-atmosphere-plugin/wiki/Home
##### Jquery. Atmosphere. js API https://github.com/Atmosphere/atmosphere/wiki/jQuery.atmosphere.js-API
##### Grails-events-push https://github.com/smaldini/grails-events-push
##### Grails plugin platform http://grailsrocks.github.com/grails-platform-core/guide/

### PS: It is too painful to read the source code of goovy language. There are only a few documents, and too many languages are involved ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.