Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use this.ctx.storage to store session data #82

Merged
merged 2 commits into from
Aug 15, 2024
Merged

Conversation

threepointone
Copy link
Collaborator

(instead of connection.state)

(instead of connection.state)
@third774
Copy link
Collaborator

third774 commented Aug 14, 2024

Hope you don't mind — I threw up a commit that I think gets this into working order the way we want. Now that room state is derived from the session storage we can't rely on connection.send() throwing errors to remove users from the room state.

The other change I made was that I noticed we were calling JSON.stringify in some cases, but not in others when setting the user state.

this.ctx.storage.put(`session-${connection.id}`, JSON.stringify(user))

I went ahead and removed this since DO storage accepts structured clone compatible data types.

I deployed this change, joined a meeting in a couple tabs, and did another deployment, and the desired outcome was achieved: the room state did not change while the durable object restarted and the WebSocket connections reconnected successfully.

@@ -172,14 +169,13 @@ export class ChatRoom extends Server<Env> {
const otherUser = await this.ctx.storage.get<User>(
`session-${data.id}`
)
this.ctx.storage.put(`session-${data.id}`, {
await this.ctx.storage.put(`session-${data.id}`, {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I really need to land the new linting rules, I'll do so next week

@threepointone
Copy link
Collaborator Author

looks good! feel free to review and land this whenever you feel like it

@threepointone threepointone marked this pull request as ready for review August 15, 2024 08:06
@threepointone threepointone changed the title WIP Use this.ctx.storage to store session data Use this.ctx.storage to store session data Aug 15, 2024
@third774 third774 merged commit 28041a0 into main Aug 15, 2024
2 checks passed
@third774 third774 deleted the use-storage-for-sessions branch August 15, 2024 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants