Chemdream
Chemdream

Reputation: 627

How do you manage websockets on an app that scales horizontally?

I have an app written in Golang, hosted on Google App Engine and uses Firestore as a backend.

Google App Engine scales horizontally.

I use the Firebase backend to send websocket messages. So it doesn't matter which App Engine Instance the user is connected to.

Every time a user authenticates, a websocket is created and a list of online users is available.

In Firestore, I simply mark that user as "Active" when their socket is connected and "Inactive" when it's disconnected.

My problem is that the app crashes (It happens, even if it didn't, I need to plan for that.) all the users that were connected to the Go Server via web socket, are still marked as active.

Web clients aren't connected to Firebase. Only to the Go server.

I can't mark all users as inactive when the app starts up. Because multiple apps might start up because of App Engine loading more instances. If I did that, users that are online would be marked as offline by new instance booting up because of load.

How does one get around issues like this?

Upvotes: 3

Views: 444

Answers (2)

Doug Stevenson
Doug Stevenson

Reputation: 317392

It's not clear from your question, but it sounds like you're hoping that the client app can manage the active state depending on the connection of the socket. That's not going to work at all, partly because of your observations here, but also because if the client loses connectivity altogether, it also won't be able to update the status on the backend.

You should manage the connection status using the websocket from the perspective of the backend. It should never lose connectivity. (This is, in fact, how Realtime Database manages its presence.)

Upvotes: 1

Hyper
Hyper

Reputation: 11

There is a way to update the db by periodically monitoring the status of the app by having a monitoring server in the middle (app-[monitor]-firebase).

Upvotes: 1

Related Questions