With grooscript grails plugin you can use spring 4 websockets support. The plugin offer 2 tags and one bean to communicate between server and the client using groovy. Your groovy code will be converted to javascript and will run in your browser. Send events in the server, and listen them in your gsp’s using websockets easy way.

Setup

You need grails 2.4+, grooscript plugin and jquery plugin. The websocket plugin do spring stuff for us, is well documented and has good support.

First add plugins in your BuildConfig.groovy:

//To work with tomcat 7
grails.tomcat.nio = true
grails.tomcat.scan.enabled = true

plugins {

    build ":tomcat:7.0.54"
    runtime ":jquery:1.11.1"
    ...
    compile ":spring-websocket:1.0.0"
    compile ':grooscript:0.7'
    ...
}

Also you have to configure grooscript plugin, to use websocket spring plugin support. In the future more events platforms will be added, as vert.x or atmosphere. So in the Config.groovy:

grooscript {
    websockets = 'springWebsocketPlugin'
}

Server side

Websocket plugin offer some options to work in the server side, I have added a new bean, that you can use in your grails artifacts, with this simple interface:

package org.grooscript.grails.websocket

interface WebsocketSender {
    //Send any object to the clients listening that path
    void send(String path, Object data)
}

For example, in a service, you can send a message to the clients:

class MyService {

    def websocketSender

    def sendToClients() {
        websocketSender.send '/topic/books', [ [title: 'groovy in action', author: 'some guys'],
                                               [title: 'grails in action', author: 'other guys :)']]
        websocketSender.send '/topic/hello', 'Hello from controller!'
    }
}

Client side

There are two tags, first tag init connection and execute any groovy when connection is up:

<!DOCTYPE html>
<html>
<head>
    <asset:javascript src="jquery" />
    <asset:javascript src="grooscript-grails" />
    <asset:javascript src="spring-websocket" />
</head>
<body>

    <grooscript:initSpringWebsocket/>

    <!-- or -->

    <grooscript:initSpringWebsocket>
        println 'Websocket is up!'
    </grooscript:initSpringWebsocket>

    <asset:deferredScripts/>

</body>
</html>

The connection between server and the client uses sockjs and stomp, see websocket plugin for more info. In javascript there is a variable called websocketClient with the stomp client object.

The other tag, is for react to events that come from the server. If you have sent an event in the server, then you can listen that event using groovy:

<grooscript:onServerEvent path="/topic/hello">
    $("#helloDiv").append '<p>'+data+'</p>' (1)
</grooscript:onServerEvent>

<grooscript:onServerEvent path="/topic/books">
    data.each { book ->
        $("#helloDiv").append '<p>'+book.title+'</p>'
    }
</grooscript:onServerEvent>

<asset:deferredScripts/> (2)
1 data is the variable with the groovy object that come from the server.
2 grooscript plugin uses asset pipeline plugin. Use this tag to process grooscript tags.

And that’s all, just use groovy in the server and in the client. Don’t have to write javascript code or worry about browser stuff. Grooscript and spring websocket plugins do the work for you.

Also, you can work with groovy objects in the client side. For example if you send from the server a list of books, and that book is a domain class like this:

class Book {

    String title
    String author

    String coolFormat() {
        "${title.split(' ').collect { it.capitalize() }.join ' '} - ${author.toUpperCase()}"
    }
}

Then, in your gsp, you can use groovy object functions:

<grooscript:model domainClass="Book"/>

<grooscript:onServerEvent path="/topic/books" type="Book">
    data.each { book ->
        $("#helloDiv").append '<p>'+book.coolFormat()+'</p>'
    }
</grooscript:onServerEvent>

Conclusion

Is very easy add websocket support to your grails 2.4 application. And now with grooscript plugin, you can create and listen to websocket events in groovy. Never was so easy add websockets to your grails applications.

You can combine websocket support with other grooscript tags to create dynamic applications in the client side.

If you missing something, or need more features, add to Github