Skip to content

Instantly share code, notes, and snippets.

@timjonesdev
Created August 21, 2019 00:30
Show Gist options
  • Save timjonesdev/60a2660ee9863052afbad3e0e7111860 to your computer and use it in GitHub Desktop.
Save timjonesdev/60a2660ee9863052afbad3e0e7111860 to your computer and use it in GitHub Desktop.
/**
* Subscribe to watch updates to a particular team
*
* @param request - must be of the form /{name}
* @return a subscription to a Server Sent Event which
* fires every time the requested team is updated in the fantasy_db.teams collection.
* The subscription is watching a change stream in MongoDB
*/
public Mono<ServerResponse> watchTeam(ServerRequest request) {
String teamName = request.pathVariable("name");
Flux<ServerSentEvent<Team>> sse = this.teamWatcher.watchForTeamChanges(teamName)
.map(team -> ServerSentEvent.<Team>builder()
.data(team)
.build());
return ServerResponse.ok().body(BodyInserters.fromServerSentEvents(sse));
}
/**
* Subscribe to watch changes for any team in the collection
*
* @param request
* @return
*/
public Mono<ServerResponse> watchTeams(ServerRequest request) {
Flux<ServerSentEvent<Team>> sse = this.teamWatcher.watchForTeamCollectionChanges()
.map(team -> ServerSentEvent.<Team>builder()
.data(team)
.build());
return ServerResponse.ok().body(BodyInserters.fromServerSentEvents(sse));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment