Google Cloud Pub/Sub adapter

How it works

This adapter uses Google Cloud Pub/Sub service to forward messages between the nodes of a Socket.IO cluster.

The source code of this adapter can be found here.

Supported features

Featuresocket.io versionSupport
Socket management4.0.0✅ YES (since version 0.1.0)
Inter-server communication4.1.0✅ YES (since version 0.1.0)
Broadcast with acknowledgements4.5.0✅ YES (since version 0.1.0)
Connection state recovery4.6.0❌ NO

Installation

  1. npm install @socket.io/gcp-pubsub-adapter

Usage

  1. import { PubSub } from "@google-cloud/pubsub";
  2. import { Server } from "socket.io";
  3. import { createAdapter } from "@socket.io/gcp-pubsub-adapter";
  4. const pubsub = new PubSub({
  5. projectId: "your-project-id"
  6. });
  7. const topic = pubsub.topic(topicNameOrId);
  8. const io = new Server({
  9. adapter: createAdapter(topic)
  10. });
  11. // wait for the creation of the pub/sub subscription
  12. await io.of("/").adapter.init();
  13. io.listen(3000);

Options

NameDescriptionDefault value
subscriptionPrefixThe prefix for the new subscription to create.socket.io
subscriptionOptionsThe options used to create the subscription.-
heartbeatIntervalThe number of ms between two heartbeats.5_000
heartbeatTimeoutThe number of ms without heartbeat before we consider a node down.10_000

Latest releases

VersionRelease dateRelease notesDiff
0.1.0March 2024link-

Complete changelog