This package provides a Redis pub/sub adapter for boardgame.io multiplayer servers.
npm install @boardgame.io/redis-pubsub
Requires redis
npm library 3.1.2
or greater (docs), and boardgame.io
version 0.47.0
or greater.
If you don’t have those installed yet, install them too:
npm install redis boardgame.io
Using a database adapter that stores and shares the state across servers is also required to properly horizontally scale them. See the boardgame.io storage docs for details.
import redis from 'redis';
import { RedisPubSub } from '@boardgame.io/redis-pubsub'
import { Server, Origins, SocketIO } from 'boardgame.io/server';
import { PostgresStore } from 'bgio-postgres';
import MyGame from './src/MyGame';
// 1. Create your Redis clients.
const pubClient = redis.createClient({ host: '192.168.8.50' });
const subClient = pubClient.duplicate();
// 2. Create an instance of the RedisPubSub adapter.
const pubSub = new RedisPubSub(pubClient, subClient);
const server = Server({
games: [MyGame],
origins: [Origins.LOCALHOST],
// 3. Pass the pub/sub instance to the SocketIO transport class.
transport: new SocketIO({ pubSub }),
db: new PostgresStore('[email protected]/vdf'),
});
server.run(8000);