Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket.IO Google Cloud pub/sub adapter

The @socket.io/gcp-pubsub-adapter package allows broadcasting packets between multiple Socket.IO servers.

Table of contents

Supported features

Feature socket.io version Support
Socket management 4.0.0 ✅ YES (since version 0.1.0)
Inter-server communication 4.1.0 ✅ YES (since version 0.1.0)
Broadcast with acknowledgements 4.5.0 ✅ YES (since version 0.1.0)
Connection state recovery 4.6.0 ❌ NO

Installation

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

Usage

import { PubSub } from "@google-cloud/pubsub";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/gcp-pubsub-adapter";

const pubsub = new PubSub({
  projectId: "your-project-id"
});

const topic = pubsub.topic(topicNameOrId);

const io = new Server({
  adapter: createAdapter(topic)
});

try {
  // wait for the creation of the pub/sub subscription
  await io.of("/").adapter.init();
} catch (err) {
  console.error(err);
  process.exitCode = 1;
}

const gracefulShutdown = async () => {
  try {
    // wait for the deletion of the pub/sub subscription
    await io.close();
  } catch (err) {
    console.error(err);
    process.exitCode = 1;
  }
};

process.once("SIGINT", gracefulShutdown);
process.once("SIGTERM", gracefulShutdown);

io.listen(3000);

Options

Name Description Default value
subscriptionPrefix The prefix for the new subscription to create. socket.io
subscriptionOptions The options used to create the subscription. -
heartbeatInterval The number of ms between two heartbeats. 5_000
heartbeatTimeout The number of ms without heartbeat before we consider a node down. 10_000

License

MIT

About

The Socket.IO adapter for Google Cloud pub/sub, allowing to broadcast events between several Socket.IO servers

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages