# grails-spring-websocket **Repository Path**: dbb_admin/grails-spring-websocket ## Basic Information - **Project Name**: grails-spring-websocket - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: 1.2.x - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-24 - **Last Updated**: 2024-11-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Websocket Grails Plugin - - - *The plugin is targeting Grails 2.4.0+. Lower versions will not work because they lack the mandatory Spring version 4.0+.* *Currently, only the Grails Tomcat Plugins (tomcat-7.0.52+/tomcat8-8.0.1.1+) are known to work with this plugin.* - - - This plugin aims at making the websocket support introduced in Spring 4.0 available to Grails applications. You can use the corresponding Spring docs/apis/samples as a reference. That is mentioned multiple times in this readme because there is everything explained in fine detail. Grails version requirements:
grails-spring-websocket Grails
1.0.x 2.4.0 - 2.4.2
1.1.x 2.4.3
1.2.x 2.4.4+
## Installation To install the plugin into a Grails application add the following line to your `BuildConfig.groovy` plugins section: compile ":spring-websocket:1.2.0" If you are using the tomcat8 plugin (8.0.1.1+), thats it. If you are using the tomcat plugin (7.0.52+), you should add the following `BuildConfig.groovy` settings to ensure proper functionality: grails.tomcat.nio = true grails.tomcat.scan.enabled = true ## Usage The plugin makes the Spring websocket/messaging web-mvc controller annotations useable in Grails controllers, too. I think basic usage is explained best by example code. But: the code below is just some very minimal it-works proof. Check the Spring docs/apis/samples for more advanced use-cases, e.g. security and authentication. ### Controller (annotated handler method) */grails-app/controllers/example/ExampleController.groovy*: ```groovy package example import org.springframework.messaging.handler.annotation.MessageMapping import org.springframework.messaging.handler.annotation.SendTo class ExampleController { def index() {} @MessageMapping("/hello") @SendTo("/topic/hello") protected String hello(String world) { return "hello from controller, ${world}!" } } ``` Unless you want your handler method to be exposed as controller action, it is important that you define the annotated method as private or protected. ### Client-side (sock.js / stomp.js) */grails-app/views/example/index.gsp*: ```gsp
``` This would be the index view of the controller above. The js connects to the message broker and subscribes to /topic/hello. For this example, i added a button allowing to trigger a send/receive roundtrip. While this example shows jquery used with the asset-pipeline plugin, the use of jquery is **not required**. If you prefer the resources plugin instead of the asset-pipeline plugin, you can use the spring-websocket resources module - it includes sock.js and stomp.js: ``` ``` ### Service (brokerMessagingTemplate bean) You can also inject and use the brokerMessagingTemplate bean to send messages directly, e.g. from a service. */grails-app/services/example/ExampleService.groovy*: ```groovy class ExampleService { def brokerMessagingTemplate void hello() { brokerMessagingTemplate.convertAndSend "/topic/hello", "hello from service!" } } ``` ## Configuration If the default values are fine for your application, you are good to go. No configuration required then. The following configuration options are available (e.g. by adding some or all of them to your `Config.groovy`):
Key grails.plugin.springwebsocket.dispatcherServlet.additionalMappings
Type Collection<String>
Default ["/stomp/*"]
Description By default, the GrailsDispatcherServlet is mapped to *.dispatch.
Because the sock.js support in Spring is not using a separate servlet but additional handlers for the DispatcherServlet, the relevant endpoints have to be covered by the servlet-mapping.
Usually, you will want to have your stomp endpoints covered.
Key grails.plugin.springwebsocket.messageBroker.applicationDestinationPrefixes
Type Collection<String>
Default["/app"]
Description Prefixes to filter destinations targeting application annotated methods.
Annotations should not contain the destination prefix.
E.g. with the default value, this means if your js client sends to /app/foo/bar, your controller annotation should look like @MessageMapping("/foo/bar").
Key grails.plugin.springwebsocket.messageBroker.userDestinationPrefix
Type String
Default "/user/"
Description The Prefix to identify user destinations.
Key grails.plugin.springwebsocket.messageBroker.brokerPrefixes
Type Collection<String>
Default ["/queue", "/topic"]
Description Prefixes to filter destinations targeting the broker.
This setting affects the direction server --> client.
E.g. with the default value, the broker would process a message to /topic/foo but not one to /unknown/prefix/foo.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.enabled
Type boolean (groovy truth)
Default false
Description If enabled, use a "real" stomp relay like RabbitMQ or ActiveMQ (with their corresponding stomp components active).
If not (default), a simple Map-based broker implementation will be used.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.host
Type String
Default 127.0.0.1
Description Only relevant if stompRelay.enabled = true.
The host of the stomp relay (IP address or hostname).
Key grails.plugin.springwebsocket.messageBroker.stompRelay.port
Type int
Default 61613
Description Only relevant if stompRelay.enabled = true.
The port of the stomp relay.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.systemLogin
Type String
Default guest
Description Only relevant if stompRelay.enabled = true.
The login of the stomp relay for the shared system connection.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.systemPasscode
Type String
Default guest
Description Only relevant if stompRelay.enabled = true.
The passcode of the stomp relay for the shared system connection.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.clientLogin
Type String
Default guest
Description Only relevant if stompRelay.enabled = true.
The login of the stomp relay for the client connections.
Key grails.plugin.springwebsocket.messageBroker.stompRelay.clientPasscode
Type String
Default guest
Description Only relevant if stompRelay.enabled = true.
The passcode of the stomp relay for the client connections.
Key grails.plugin.springwebsocket.stompEndpoints
Type Collection<Collection<String>>
Default [["/stomp"]]
Description Expose a STOMP endpoint at the specified url path (or paths).
For every inner Collection, a stomp endpoint is registered with those url path(s). E.g. with the default value, one stomp endpoint is registered and listening at /stomp
Key grails.plugin.springwebsocket.clientInboundChannel.threadPoolSize
Type Range<Integer>
Default 4..10
Description Core to max thread pool size for the TaskExecutor of the client inbound channel (client --> server)
Key grails.plugin.springwebsocket.clientOutboundChannel.threadPoolSize
Type Range<Integer>
Default 4..10
Description Core to max thread pool size for the TaskExecutor of the client outbound channel (server --> client)
Key grails.plugin.springwebsocket.useCustomConfig
Type boolean (groovy truth)
Default false
Description Set this to true if you want to take full control and responsibility for the spring websocket configuration.
Then, all other config options above will have no effect.
Neither the WebSocketConfig nor the GrailsSimpAnnotationMethodMessageHandler will be exposed to the application.
If you need more sophisticated configuration options, currently the way to go would be using the useCustomConfig setting and heading over to the Spring docs/apis/samples covering the configuration of websockets/messaging. You can of course use the plugin's `WebSocketConfig` for orientation. It uses `@EnableWebSocketMessageBroker` and implements `WebSocketMessageBrokerConfigurer`. But for bigger config adjustments, it is likely you end up extending Spring's `WebSocketMessageBrokerConfigurationSupport`. Future versions of this plugin may cover more configuration options.