简体   繁体   中英

Proper way to create activity feed in Spring

In a section of my page, I need to have ActivityFeed which shows latest entered data from a user.

So there's the User model.. And the Survey model.

The user gives some input, and then they show up on the Survey table. The Survey model has a field with a timestamp

@CreationTimestamp
@Column(name = "submitedTime",nullable=false)
@Temporal(TemporalType.TIMESTAMP)
private Date submittedDate;

So what I did is on the SurveyRepository called a method :

List<Survey> findTop10ByOrderBySubmittedDateDesc();

And this returns me the 10 latest submitted surveys. Then I create an endpoint for this :

  @RequestMapping(value="getActivityFeed",method=RequestMethod.GET)
public @ResponseBody List<Survey> getSurveys(){
    return surveyService.findTop10ByOrderBySubmittedDateDes();
}

Then with an Ajax Call from my JS I call the Endpoint, and pull out information.

Is this the proper way to create an Activity feed? Or is there a way that after every submitted value it, the Activity Feed gets updated ?

I am using Spring-Boot and Thymeleaf too.

THanks

I suppose you are using a setInterval to fetch the recent activities via Ajax. That is ok, but a better way of doing that is using websocket(avoiding users polling your server consuming precious resources):

WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server.

(MDN Webdocs - https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API )

It is very lightweight for you purpose, and I believe it is the best choice for updating user's view based on real backend events.

Take a better look at Mozilla's reference and check these other references as well:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM