I need to push some data to the client if it is in Redis, but client keeps reconnecting to the SSE endpoint every 5 seconds.
The backend code:
@RestController
@RequestMapping("/reactive-task")
public class TaskRedisController {
private final TaskRedisRepository taskRedisRepository;
TaskRedisController(TaskRedisRepository taskRedisRepository){
this.taskRedisRepository = taskRedisRepository;
}
@CrossOrigin
@GetMapping(value = "/get/{id}")
public Flux<ServerSentEvent<Task>> getSseStream2(@PathVariable("id") String id) {
return taskRedisRepository.findByTaskId(id)
.map(task -> ServerSentEvent.<Task>builder().data(task).build());
}
}
@Repository
public class TaskRedisRepository {
public Flux<Task> findByTaskId(String id) {
return template.keys("task:" + id).flatMap(template.opsForValue()::get);
}
}
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@Entity
public class Task {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(length = 25)
private String result;
}
The client consumes using JS:
var evt = new EventSource("http://localhost:8080/reactive-task/get/98"); evt.onmessage = function(event) {
console.log(event);
};
Can anyone point me in the right direction?
Any advice would be appreciated.
Update: I need to store data for some time (5-10 mins) in Redis. Update: I wrote similar code on MongoDB and it works fine.