0

I'm developing a game using AWS Amplify. The game state will be stored in DynamoDB tables and will be queried and modified with GraphQL. There isn't a pressing need for realtime or low-latency communication; However, I need to detect when a player joins or disconnects from a game. What's the best mechanism for implementing this?

What I had in mind was an event that fires when a WebSocket connection is established or broken. The best I could glean from the Amplify docs was using PubSub with AWS IoT, but I don't know if this will work. If possible, I would like to avoid incurring additional API costs.

I already implemented a version of this where the client updates a lastSeen field in the database every 30 seconds or so but it felt pretty janky.

5nefarious
  • 347
  • 1
  • 3
  • 11

2 Answers2

1

I think you need to distinguish between a disconnect and inactivity. Somebody may simply be inactive and in that case you would disconnect them yourself after they did nothing for x amount of time.

A disconnect on the other hand should notify your server that is indeed what the user intended to do.

Derrops
  • 7,651
  • 5
  • 30
  • 60
  • For my purposes, I don't think there's going to be a meaningful difference between disconnect and inactivity, but thanks for bringing that up. I'll keep that in mind. Detecting inactivity is the real problem, as I can always have the client update the database before a disconnect. – 5nefarious Dec 30 '20 at 23:21
  • I'm not super familiar with websockets but I believe most of the time the servers/frameworks do support parameters to handle this and configure timeouts. – Derrops Dec 31 '20 at 03:55
  • This is a serverless architecture, which is what makes this a little more challenging. By default, the connection is stateless. – 5nefarious Jan 01 '21 at 17:27
1

I think DataStore Events will do what you want. They have a specific network status event you can use to trigger state changes.

Jim J
  • 546
  • 3
  • 11
  • I used DataStore initially but switched to GraphQL. Wouldn't adding DataStore mean I'd have to completely rewrite how the app handles API calls? – 5nefarious Dec 30 '20 at 23:19
  • Maybe, I'm not sure if you actually need to switch over your other calls. It might be enough to include DataStore and subscribe to its Hub events, or have some sort of dummy tiny-as-possible object you load via DataStore, apart from your other graphql calls, that serves as a beacon. Crude hack but it might get you unblocked and be marginally better than `lastSeen` every 30 seconds. – Jim J Dec 31 '20 at 20:04
  • 1
    YKW I just realized you wanted server-side, my mistake. I think the hub events are strictly client-side. – Jim J Dec 31 '20 at 23:03
  • That makes sense. I believe the server sends heartbeat messages to the AppSync client, so DataStore is probably using that. But, yes, I am looking for something on the server side. – 5nefarious Jan 01 '21 at 17:25