Database & Persistance
Colyseus is agnostic to the database you use. You can use your preferred Node.js tool for working with databases.
⚠️
We’d love to ship Colyseus 1.0 with our own database solution. We’ve been experimenting with our own tool built on top of Kysely. If you’re interested in helping/contributing, please join the discussion and let us know!
Recommended database tools
Query builders
Query builders are known for their simplicity and flexibility.
ORMs (Object-Relational Mappers)
ORMs are known for abstracting the database layer and providing a more object-oriented approach.
Common Usage Patterns
Query the database on onAuth
method
You may fetch user data based on the options
passed to the client on connection, or based on the authentication token.
src/rooms/MyRoom.ts
// ...
async onAuth(client, options) {
const user = await db.query('SELECT * FROM users WHERE id = ?', [options.userId]);
return user;
}
onJoin (client, options) {
// ...
await db.query('UPDATE users SET online = true WHERE id = ?', [client.auth.id]);
}
Update the database on onLeave
method
You may update the database when the client leaves the room.
src/rooms/MyRoom.ts
// ...
async onLeave(client, consented) {
// ...
await db.query('UPDATE users SET online = false WHERE id = ?', [client.auth.id]);
}
Last updated on