Push Notification in Spring MVC WebApp - java

I am currently writing a forum web-application using Spring MVC. I'm just a beginner at Spring and have only been working with it for about 1 week now.
I need to implement push notifications. Here is the scenario: User A logs in and creates a post. User B comments on User A's post while User A is still logged in. User A receives a notification that some user has commented on his post, without his browser refreshing the page.
I need help with sending the notification to User A that User B has commented on his post asynchronously. I have done some research, and found that there is a package called CometD that I can use, but I cannot find any simple tutorials for me to understand.
Can anyone suggest any other packages/way to solve my problem? Or if you have any simple CometD tutorials, that would be great as well.

I am the CometD project lead.
CometD ships with a demo war file that has a built-in chat application that you can use as a basis to understand how CometD works, see https://docs.cometd.org/current/reference/#_installation.
The CometD full reference documentation is here: https://docs.cometd.org.
From experience, I don't recommend using Servlet 3.0 async features to implement a chat, because that will rule out usage of WebSocket.
It's better you rely on frameworks such as CometD that can start with WebSocket (as it is more efficient) and then fallback automatically and transparently to HTTP if WebSocket does not work.
On a similar note, I don't recommend a WebSocket only approach because WebSocket does not work in certain scenarios (for example, mobile networks often do not allow WebSocket traffic), and it's a too low-level protocol so you will have to build your own protocol on top of WebSocket (which is a lot of work and not easy to get right).
CometD provides you with an easy to extend protocol and automatic transport fallback, and has a ton of features (e.g. authorization & authentication and clustering just to mention two).

Have a look at the spring MVC chat sample app for spring MVC 3.2, there is also a spring blog post about it.

Related

REST API with websocket using Spring boot

I am actually working to my university junior project.
I want to make a game using spring boot, this game will need a WebSocket.
I will surely need to make a REST API for user registration, deleting, updating, posting and getting data.
Am I going to be able to use WebSocket along with REST?
If yes please send me resources that can help me, otherwise please tell me what should I do.
I'm sorry if my question was not so smart but I am new to all of this.
Yes, your Spring application can easily use both WebSocket and REST APIs. If you're going to use Spring Boot, I'd suggest to take a look into their example projects (note the spring-boot-sample-websocket- directories). I think it's the best way to learn.
More on WebSockets, I'd suggest to use STOMP protocol. There is also a nice guide for it on how you can get started.
For your RESTful API, you'll be making simple controllers / services, there is tones of resources for this. You can also follow this guide to get started as well.
While Spring applications support both Websocket and REST API's individually, you can use Websocket connection to send requests to your rest API's too. Needs a little glitching tho, but I've made it work in this github repo. Using this library you can represent your rest-api's in a websocket based protocol.
I also made java (and springboot starter) for client.

Facebook Messenger bot using Spring Boot?

Nowadays more and more chat bots are being created, as the process is relatively easy if you activate a facebook developer account, and create a facebook page for this purpose.
Probably the most popular method is to using NodeJS - Heroku, as it is explained in the following article:
https://chatbotslife.com/how-to-make-a-facebook-messenger-chat-bot-in-1hr-af6bec5e7aec#.xqgu2lb46
After a research I found that making connection with facebook IS possible via Spring boot (as explained here: https://spring.io/guides/gs/accessing-facebook/ ) but I have not found any information or - even similar to the NodeJS - method to handle a messenger conversation using a Spring Boot server.
Is it possible to request and send facebook messenger messages from Spring Boot? I would prefer that over NodeJS, as I have a system already built in Spring Boot, and it would be much easier to integrate this feature there, than creating a new service, which would be then connected to the existing one. (even if in some cases I definitely would stuck with the 'microservice' structure.)
It is possible.
Cast a glance onto that project:
https://github.com/messenger4j/messenger4j-spring-boot-quickstart-template
Though I understand your reason why you want to 'keep things together', I definitely would recommend to separate things to smaller pieces when you can, as it is a much better strategy generally.

Can Spring XD be used as a message broker for notifications?

I have to implement a notification system within a Java Spring app with angular for a front end. I was advised on using Spring XD as the message broker. However after looking at it, I am unsure if this would be a good strategy. It might be possible but will look like a hack.
EDIT: I have a simple use-case. On the web app if user A does an action X, then I need to notify user B about action X using notifications in the web app (if the user B is currently logged in), through GCM and through SMS.
AFAIK, Spring XD is a framework which allows us to communicate with several different Message Broker (among other things that it does). So, Spring XD itself cannot act as a MB Server. As a MB server, you have several options like ActiveMQ, RabbitMQ, Kafka....
If you are planning to build a message oriented system from ground up, then Spring XD could be a good choice. If you are just looking for adding a new feature in existing application, you can achieve communication to any of the above MQ servers using Spring Integration (recommend you check Spring Integration Java DSL). This way you will be easily integrate it with existing application.

Alerting user between application event

I'm not really sure how to describe the design issue I'm having so the title is very vague and possibly just totally wrong.
Basically, I have a web application which uses a number of different payment gateways, depending upon which one the user prefers.
Obviously the communication between the application and the gateway endpoint is going to be different for each. I need the way for my application to react to the response of the gateway by the means of a javascript alert (for now). The gateways might have different return url's back to the application, and it needs to work between browser sessions.
Any ideas or pointers to technology that will help with this?
If it helps the technology stack im using: Java, Spring, Struts
Thanks,
I would recommend a dedicated messaging layer with Redis, Socket.io or Rabbit.mq. I had the same issue with a medical app. If you want to stick to Java i would recommend an Observer pattern to pass event messages between classes but you still need a way to distribute notifications to the presentation layer handlers (Javascript) and thats were I recommend a socket server of sorts

Frameworks for Monitoring Application

We're starting work on a monitoring application that will be providing status for several pieces of hardware. The data will be updated on an Oracle DB by a backend process, this application will have to be developed in Java and deployed on a WebLogic 12c application server stuck behind an Apache HTTPD server.
Since the monitoring will be real-time, we'll be needing almost instant refresh of the data that was added on the database. To achieve this, I've been doing some tests with the following frameworks:
Hibernate as ORM.
Spring 3.0.5.
JSF 2.2 and PrimeFaces 3.5 with PrimeFaces Push (Uses Atmosphere).
My tests were successful and I was able to get something working quite quickly. However, I've noticed that Atmosphere has some potential that is being contained by the PF implementation, also, I don't know if this is the most lightweight solution.
Because of this, I wanted to ask you if you guys had any other approaches for this solution, i.e:
Spring MVC + Atmosphere.
Other MVC + Other Comet/WebSocket framework.
Most of the data shown will be for charts, tables and maps. The biggest limitation will be that everything must be Java compatible, since the application server won't be changed.
To summarize, my question is:
Which alternative options do you suggest for this project based on the requirements and limitations I established?
Let me know if you need additional information.
Spring 4 has good Websockets support, have a look here at the documentation. It makes possible to create a websockets endpoint, and use it in a publish-subscribe model based on the STOMP protocol.
This protocol is a messaging protocol that allows to subscribe to a topic and receive notifications, publish to a topic to all subscribed listeners or send a server push message to a single client.
Currently Websockets might not work for all users due to browser limitations, proxies that are not configured to forward the upgrade headers needed to upgrade from HTTP to Websockets, or network elements that timeout long-lived connections.
Spring 4 provides good support for this via SockJS, which has transparent fallback capabilities (to ajax, iframe) if a Websocket connection cannot be established.
All these things are needed to use Websockets in this day and age, and Spring 4 provides a complete out-of-the-box solution.
Have a look at this blog post to see how lightweight this solution is, specially if used together with Spring Boot.
If it's a frontend intensive application you might to couple Spring 4 with AngularJs, for frontend widget development. But Primefaces is a great solution, if the widgets they provide suit your needs it would spare a lot of work.

Categories