I am using MongoDB to back my multiplayer game app. I am using a changeStream to notify clients of the game state every time there are updates:
games = client.db().collection('games')
games.watch({fullDocument: 'updateLookup'}).on('change', data => {
const game = data.fullDocument
// broadcast game to relevant clients
})
The issue with this is that clients will only get updates when there are changes to their game, but they also need a way to get the current state of the game when they initially connect to the server. Otherwise, they have to wait until someone makes an update to the game state before receiving the game document.
I want something like RethingDB's initial values feature for their changefeeds. ZeroMQ also describes a similar pattern which they call last value caching.
What's the best way to implement this pattern with MongoDB?
The obvious workaround is to have clients execute a findOne
query upon initially connecting to a game, but then the clients would need a way to resolve ordering between the changestream updates and the findOne
result.