I have a project which consists of the following parts:
- Rest API (with Spring)
- web dashboard (uses the Rest API to manage data)
- Client (fetches data and works with it)
Now what im asking myself is, how can I make sure that the client fetches the new data as soon as it as updated through the web dashboard? Fetching every few seconds doesn‘t seem like the right approach, would a socket connection be a good solution?
Please let me know if you have any ideas, thank you.
CodePudding user response:
As an option, you could send a message to a queue when a table row is created/updated/deleted. With that, you will have the changes asynchronously and you can react based on them.
In the publisher side you can have something like this:
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@EntityListeners(ExamplePersonEntityListener.class)
class ExamplePerson {
@Id
@GeneratedValue(strategy=IDENTITY)
private Long id;
private String name;
private String lastname;
}
@Configurable
class ExamplePersonEntityListener extends AbstractEntityyListener<ExamplePersonEntityApplicationEvent> {
@PostPersist
public void examplePersonCreated(ExamplePerson model) {
this.broadcast(new ExamplePersonEntityApplicationEvent(EventActionn.CREATE, model));
}
@PostUpdate
public void examplePersonUpdated(ExamplePerson model) {
this.broadcast(new ExamplePersonEntityApplicationEvent(EventActionn.UPDATE, model));
}
@PostRemove
public void examplePersonRemoved(ExamplePerson model) {
this.broadcast(new ExamplePersonEntityApplicationEvent(EventActionn.DELETE, model));
}
}
public class ExamplePersonEntityApplicationEvent extends EntityApplicationEventt<ExamplePerson> {
protected ExamplePersonEntityApplicationEvent(EventActionn action, ExamplePerson model) {
super(action, model);
}
}
@Configurable
abstract class AbstractEntityyListener<T extends EntityApplicationEventt<?>> {
@Autowired
private ObjectFactory<ApplicationEventPublisher> appEvents;
public final void setApplicationEventPublisher(ObjectFactory<ApplicationEventPublisher> appEvents) {
this.appEvents = appEvents;
}
protected final void broadcast(T event) {
assert this.appEvents != null;
this.appEvents.getObject().publishEvent(event);
}
}
class EntityApplicationEventt<T extends Object> extends ApplicationEvent {
private final EventActionn action;
private final T model;
protected EntityApplicationEventt(EventActionn action, T model) {
super(model);
this.action = action;
this.model = model;
}
public EventActionn getAction() {
return this.action;
}
public Long getId() {
return (Long)this.getSource();
}
public T getModel() {
return this.model;
}
}
enum EventActionn {
CREATE,
UPDATE,
DELETE,
}
You will need to create a queue and the subscriber to the queue on the Client.
CodePudding user response:
In general, there are two ways:
- Push solution (As you mentioned) Ex: WebSocket/streaming
- Using a webhook. You have to implement it yourself or use a webhook provider.