Skip to content

Conversation

@aryasaatvik
Copy link
Contributor

Problem

When extending YServer to handle custom string messages alongside Yjs binary messages, developers couldn't override onMessage and call super.onMessage() because it was defined as a class field rather than a method.

// This didn't work ❌
class MyYServer extends YServer {
  onMessage(conn, message) {
    if (typeof message === "string") {
      // handle custom JSON
      return;
    }
    super.onMessage(conn, message); // undefined - class fields aren't on prototype
  }
}

Solution

Refactored onMessage from a class field to a proper method, moving the chunking handler to a private handleChunkedMessage field.

// Now works ✓
class MyYServer extends YServer {
  onMessage(conn, message) {
    if (typeof message === "string") {
      const data = JSON.parse(message);
      // handle custom messages
      return;
    }
    super.onMessage(conn, message); // properly delegates to parent
  }
}

This enables mixed message handling (custom JSON + Yjs protocol) without modifying the library internals.

Refactors YServer.onMessage from a class field to a proper method,
enabling subclasses to override it and call super.onMessage().

Previously, onMessage was assigned as a class field (onMessage = handleChunked(...)),
which prevented proper inheritance - super.onMessage was undefined because class
fields don't exist on the prototype chain.

This change allows users to extend YServer and handle custom string messages
alongside the default binary Yjs protocol messages.

Example usage:
```typescript
class MyYServer extends YServer {
  onMessage(conn, message) {
    if (typeof message === "string") {
      // Handle custom JSON messages
      return;
    }
    super.onMessage(conn, message); // Now works!
  }
}
```
@changeset-bot
Copy link

changeset-bot bot commented Oct 18, 2025

⚠️ No Changeset found

Latest commit: 794ce47

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@threepointone
Copy link
Collaborator

actually, how about this as a better first class solution #290

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