DO websocket with DOSQLite

We're building off of the DO chat sample. We're trying to create a single websocket per user so they can switch between their chats. BUT currently, if a user connects with their chatid for the chatroomObject, that wont allow the user to swap between their chats while still being connected via websocket because the entire DO object is based off of the chatid. Would it be dumb to use <app-name> as the chatid for all users and store ALL chat messages in one table and query that via DOSQLite? this.sql.exec('CREATE TABLE IF NOT EXISTS chat_message( _id INTEGER PRIMARY KEY AUTOINCREMENT, userId INTEGER NOT NULL, message TEXT NOT NULL, createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, isDeleted BOOLEAN NOT NULL DEFAULT 0, chatId INTEGER NOT NULL );')
1 Reply
Gagan Suie
Gagan SuieOP•2d ago
i guess the problem with this approach is that if we ever hit a large amount of users. they would all be stored in sessions array. which may be a lot of connections. 🙃 we ended up optimizing our implementation by using a UserSession DO that handles single websockets per user and combined with a ChatRoom DO that handles all the chat functionality and DOSQLite. pretty elegant solution that handles scaling well.

Did you find this page helpful?