user notifications in Grails - grails

I'm building an application using Grails 2.5.1 , i want to implement a user's notification service ,similar as Stack overflow for instance when the user has unread messages it notifies the user as soon as he login . is there a plugin or a handy way to achieve this ?
thanks

If you want some data (ex: unread messages) on demand (login), then you could include this in the action's returned map or fetch the data from a separate Ajax call when the document.event fires and manipulate the DOM (easily done through jquery, angular, etc.)
If you are looking to update the DOM asynchronously based on events that happen server-side (another user sends a message and you would like to 'instantly' alert the current user), then things become more complicated.
Spring Websocket
There is a grails plugin that we have experimented with and have had success with: grails-spring-websocket. Check out the link for examples and more info.
There is a bean brokerMessagingTemplate that is injected within your service class that has methods to publish a message. On the client, you subscribe to the corresponding message url using javascript. When the callback function is executed, a message has been published - manipulate the DOM as needed.
There are also some controller annotations provided by the plugin, but I don't have experience with them.

Related

How to dynamically and efficiently pull information from database (notifications) in Rails

I am working in a Rails application and below is the scenario requiring a solution.
I'm doing some time consuming processes in the background using Sidekiq and saves the related information in the database. Now when each of the process gets completed, we would like to show notifications in a separate area saying that the process has been completed.
So, the notifications area really need to pull things from the back-end (This notification area will be available in every page) and show it dynamically. So, I thought Ajax must be an option. But, I don't know how to trigger it for a particular area only. Or is there any other option by which Client can fetch dynamic content from the server efficiently without creating much traffic.
I know it would be a broad topic to say about. But any relevant info would be greatly appreciated. Thanks :)
You're looking at a perpetual connection (either using SSE's or Websockets), something Rails has started to look at with ActionController::Live
Live
You're looking for "live" connectivity:
"Live" functionality works by keeping a connection open
between your app and the server. Rails is an HTTP request-based
framework, meaning it only sends responses to requests. The way to
send live data is to keep the response open (using a perpetual connection), which allows you to send updated data to your page on its
own timescale
The way to do this is to use a front-end method to keep the connection "live", and a back-end stack to serve the updates. The front-end will need either SSE's or a websocket, which you'll connect with use of JS
The SEE's and websockets basically give you access to the server out of the scope of "normal" requests (they use text/event-stream content / mime type)
Recommendation
We use a service called pusher
This basically creates a third-party websocket service, to which you can push updates. Once the service receives the updates, it will send it to any channels which are connected to it. You can split the channels it broadcasts to using the pub/sub pattern
I'd recommend using this service directly (they have a Rails gem) (I'm not affiliated with them), as well as providing a super simple API
Other than that, you should look at the ActionController::Live functionality of Rails
The answer suggested in the comment by #h0lyalg0rithm is an option to go.
However, primitive options are.
Use setinterval in javascript to perform a task every x seconds. Say polling.
Use jQuery or native ajax to poll for information to a controller/action via route and have the controller push data as JSON.
Use document.getElementById or jQuery to update data on the page.

Grails and Atmosphere: Rendering templates in a service using Atmosphere push

I'm using Atmosphere 1.0.12 to push data from a server to my clients in a Grails 2.2.1 application.
The application is an auction Web site, and I'm pushing updates such as prices, bidder information, and status messages. The goal is to change information dynamically on the page without requiring a page reload. Some of the information is very easy to update, such as a price change after a bid. But other things require some logic, such as telling a user they are the high bidder, or have been outbid.
Currently I've implemented an Atmosphere service (LotPushService) to push the information.
However, after these updates, certain information is missing, such as user information and locale, because this isn't sent along with the updates and isn't being handled by the service.
Is it possible to use the service to render page templates when it sends the push update, so that session and state information is preserved?
Are there any best practices for this situation?
Have you configured the BroadcasterCache? At least you should in order to not lose messages.

Capture outgoing HTTP request from Controller / Service

So I have the following scenario (it's a Grails 2.1 app):
I have a Controller that can be accessed via //localhost:8080/myController
This controller in turn executes a call to another URL opening a connection using new URL("https://my.other.url").openConnection()
I want to capture the request so I can log the information
I have a Filter present in my web.xml already which does the job well for controllers mapped in my app. But as soon as a request is fired to an external URL, I don't get anything.
I understand that my filter will only be invoked to URLs inside my app, and that depends on my filter mapping which is fine.
I'm struggling to see how a solution inside the app is actually viable. I'm thinking of using a mixed approach with the DevOps team to capture such outgoing calls from the container and then log them into a separate file.
I guess my questions are:
Is there a way to do it inside the app itself?
Is the approach I'm planning a sensible one?
Cheers!
Any reason why you don't want to use http-builder? There a Grails plugin for it, and it makes remote XML calls much easier than handling the plumbing yourself. At the bottom of the linked page they describe how you can enable request logging via log4j configuration.

ASP.NET MVC's AsyncController and Session data

We have a somewhat complex and long-running action method for which we'd like to show a progress bar. The javascript grid that we're using requires that saving and loading data be done in a single request (save first then load), and also requires us saving data to session.
Our initial thought was to just use an AsyncController, but the method obviously failed since it accesses (and saves to) session state. Since we cannot split up the action due to the javascript grid, is there any other option?
You could use a service bus and send a message to this. Granted this might be overkill for what you are doing but could be worth a look.
Search SO for rhinobus, masstransit or nservicebus. These would allow you to send a message asynchronously but you have to setup the application that subscribes to this message.

Pushing updates to the view in (close to) real-time

Say my user is viewing messages/index, and someone else sends him a message. I'd like the user's view to update in real-time to reflect the new message, kind of like how Twitter lets me know there are more messages on my timeline. Are there any examples of this being done in Rails?
You can either use AJAX to poll the server for updates on a regular basis (pull model), or use the Juggernaut plugin or similar to enable the server to send updates to the client (push model). Note that this requires Flash to be installed on the client.
DUI.stream from digg might be the solution you are looking for. It keeps an open xhr stream that you can keep on adding objects to to send to the user. It has a ruby client example

Resources