register(config: RegisterConfig): Promise<CoordinatorClient>;
Registers backend with the Hathora Coordinator.
export type RegisterConfig = {
coordinatorHost?: string;
appSecret: string;
storeId?: StoreId;
authInfo: AuthInfo;
store: Store;
};
The url of the coordinator instance to connect to
Defaults to coordinator.hathora.dev
A secret string value to securely identify the backend
A string to identify the backend instance
Defaults to a random uuid
Configures the authentication providers for the application
type AuthInfo = {
anonymous?: { separator: string };
nickname?: {};
google?: { clientId: string };
email?: { secretApiKey: string };
};
A class or object conforming to the Store
interface
interface Store {
newState(roomId: RoomId, userId: UserId, data: ArrayBufferView): void;
subscribeUser(roomId: RoomId, userId: UserId): void;
unsubscribeUser(roomId: RoomId, userId: UserId): void;
onMessage(roomId: RoomId, userId: UserId, data: ArrayBufferView): void;
}
interface CoordinatorClient {
sendMessage(roomId: StateId, userId: UserId, data: Buffer): void;
broadcastMessage(roomId: RoomId, data: Buffer): void;
closeConnection(roomId: StateId, userId: UserId, error: string): void;
getSubscribers(roomId: RoomId): UserId[];
ping();
}
const coordinator = await register({
appSecret: process.env.APP_SECRET!,
authInfo: { anonymous: { separator: "-" } },
store: {
newState(roomId, userId, data) {
console.log("newState", roomId.toString(36), userId, data);
},
subscribeUser(roomId, userId) {
console.log("subscribeUser", roomId.toString(36), userId);
},
unsubscribeUser(roomId, userId) {
console.log("unsubscribeUser", roomId.toString(36), userId);
},
onMessage(roomId, userId, data) {
const dataBuf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
console.log("handleUpdate", roomId.toString(36), userId, dataBuf.toString("utf8"));
// echo data back to client
coordinator.stateUpdate(roomId, userId, dataBuf);
},
},
});