Haven’t deployed it on Fly yet, but you can check the code.
The app is a Fastify Node.js server that roughly works this way:
When the server starts, it checks whether a PubSub particular topic exists. If not, the topic is created.
Each VM of the Fly app will have its own subscription. This ensures each PubSub message sent to the topic is received by all VMs.
When the VM is shut down, the subscription is deleted. If there was a problem and this didn’t happen, the subscription would be deleted by Google anyway after a couple of days of no one acknowledging the messages.
When the server starts, it starts sending a message to the PubSub topic at random intervals with the server ID (max 10 seconds). This message is received by all subscribers and sent to all the WS connected clients.
The front end is served by this server too. It connects to the WS server, and displays the WS messages received:
Oh this sounds cool. Have you looked at open source options for events/pubsub? I’ve been meaning to try a Node app with nats.io running in the same VM. I really love apps that don’t require external services. In Go, I’d just embed NATS. I’m curious if there’s a way to get similarly simple with Node.
Ooof I’m way behind, sorry for being so slow to respond to this. This looks really good. Do you want to post it on your blog (we’ll promote it) or have us run it on ours?