I’m working on an article about using WS + PubSub with Fly.
Here’s the repo of the demo app: GitHub - PierBover/fly-web-socket-google-cloud-pub-sub
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:
Now for the article, this is roughly the content I have in mind:
- Introduction to Fly
- How WebSockets work in Fly
- Strategies for integrating PubSub with Fly
- Ensuring a message is sent once to a region
- Ensuring a message is sent once to all VMs
- The demo app
- How it works
- How to get it up and running on Fly
Any suggestions/ideas are welcome!