Skip to content

Commit c227c71

Browse files
authored
fix: ensure y-websocket is default provider in sdks (#2535)
1 parent bafd1ce commit c227c71

5 files changed

Lines changed: 136 additions & 7 deletions

File tree

apps/cli/src/commands/open.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { getBooleanOption, getNumberOption, getStringOption, resolveDocArg, resolveJsonInput } from '../lib/args';
2-
import { parseCollaborationInput, resolveCollaborationProfile } from '../lib/collaboration';
2+
import {
3+
buildShorthandCollaborationInput,
4+
parseCollaborationInput,
5+
resolveCollaborationProfile,
6+
} from '../lib/collaboration';
37
import {
48
getProjectRoot,
59
createInitialContextMetadata,
@@ -105,12 +109,11 @@ export async function runOpen(tokens: string[], context: CommandContext): Promis
105109
payload.bootstrapSettlingMs = bootstrapSettlingMs;
106110
collaborationInput = parseCollaborationInput(payload);
107111
} else if (collabUrl) {
108-
collaborationInput = parseCollaborationInput({
109-
providerType: 'hocuspocus',
112+
collaborationInput = buildShorthandCollaborationInput({
110113
url: collabUrl,
111114
documentId: collabDocumentId,
112-
...(onMissing != null ? { onMissing } : {}),
113-
...(bootstrapSettlingMs != null ? { bootstrapSettlingMs } : {}),
115+
onMissing,
116+
bootstrapSettlingMs,
114117
});
115118
} else if (collabDocumentId) {
116119
throw new CliError('MISSING_REQUIRED', 'open: --collab-document-id requires --collab-url.');
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { describe, expect, test } from 'bun:test';
2+
import {
3+
buildShorthandCollaborationInput,
4+
DEFAULT_SHORTHAND_COLLABORATION_PROVIDER_TYPE,
5+
parseCollaborationInput,
6+
resolveCollaborationProfile,
7+
} from '../collaboration';
8+
9+
describe('buildShorthandCollaborationInput', () => {
10+
test('uses y-websocket as the default provider type', () => {
11+
expect(DEFAULT_SHORTHAND_COLLABORATION_PROVIDER_TYPE).toBe('y-websocket');
12+
});
13+
14+
test('returns a validated CollaborationInput with all params', () => {
15+
const input = buildShorthandCollaborationInput({
16+
url: 'ws://localhost:4000',
17+
documentId: 'my-doc-room',
18+
onMissing: 'error',
19+
bootstrapSettlingMs: 2000,
20+
});
21+
22+
expect(input).toEqual({
23+
providerType: 'y-websocket',
24+
url: 'ws://localhost:4000',
25+
documentId: 'my-doc-room',
26+
onMissing: 'error',
27+
bootstrapSettlingMs: 2000,
28+
});
29+
});
30+
31+
test('handles minimal params (url-only)', () => {
32+
const input = buildShorthandCollaborationInput({ url: 'ws://localhost:4000' });
33+
34+
expect(input.providerType).toBe('y-websocket');
35+
expect(input.url).toBe('ws://localhost:4000');
36+
expect(input.documentId).toBeUndefined();
37+
expect(input.onMissing).toBeUndefined();
38+
expect(input.bootstrapSettlingMs).toBeUndefined();
39+
});
40+
41+
test('flows through to resolveCollaborationProfile with correct provider', () => {
42+
const input = buildShorthandCollaborationInput({
43+
url: 'ws://localhost:4000',
44+
documentId: 'my-doc-room',
45+
});
46+
const profile = resolveCollaborationProfile(input, 'fallback-session');
47+
48+
expect(profile.providerType).toBe('y-websocket');
49+
expect(profile.documentId).toBe('my-doc-room');
50+
});
51+
});
52+
53+
describe('parseCollaborationInput', () => {
54+
test('accepts explicit hocuspocus provider type', () => {
55+
const input = parseCollaborationInput({
56+
providerType: 'hocuspocus',
57+
url: 'ws://localhost:1234',
58+
documentId: 'room-1',
59+
});
60+
61+
expect(input.providerType).toBe('hocuspocus');
62+
});
63+
});

apps/cli/src/lib/collaboration.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type CollaborationRuntime = {
4343
dispose(): void;
4444
};
4545

46+
export const DEFAULT_SHORTHAND_COLLABORATION_PROVIDER_TYPE: CollaborationProviderType = 'y-websocket';
4647
const DEFAULT_SYNC_TIMEOUT_MS = 10_000;
4748
const SYNC_POLL_INTERVAL_MS = 25;
4849
const ENV_VAR_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
@@ -140,6 +141,18 @@ export function resolveCollaborationProfile(input: CollaborationInput, sessionId
140141
};
141142
}
142143

144+
export function buildShorthandCollaborationInput(params: {
145+
url: string;
146+
documentId?: string;
147+
onMissing?: string;
148+
bootstrapSettlingMs?: number;
149+
}): CollaborationInput {
150+
return parseCollaborationInput({
151+
providerType: DEFAULT_SHORTHAND_COLLABORATION_PROVIDER_TYPE,
152+
...params,
153+
});
154+
}
155+
143156
export function resolveCollaborationToken(profile: CollaborationProfile): string | undefined {
144157
if (!profile.tokenEnv) return undefined;
145158
const token = process.env[profile.tokenEnv];

apps/docs/document-engine/sdks.mdx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ Use this when your app already has a live collaboration room (Liveblocks, Hocusp
204204
205205
Pass `collabUrl` and `collabDocumentId` to `client.open`:
206206
207+
<Note>
208+
The `collabUrl` + `collabDocumentId` shorthand defaults to the `y-websocket` provider. To connect to a
209+
Hocuspocus server, pass an explicit `collaboration` object with `providerType: 'hocuspocus'`.
210+
</Note>
211+
207212
<Tabs>
208213
<Tab title="Node.js">
209214
```typescript
@@ -248,6 +253,35 @@ Pass `collabUrl` and `collabDocumentId` to `client.open`:
248253
</Tab>
249254
</Tabs>
250255
256+
### Connect to Hocuspocus explicitly
257+
258+
Use the explicit `collaboration` object when your server speaks the Hocuspocus protocol instead of `y-websocket`:
259+
260+
<Tabs>
261+
<Tab title="Node.js">
262+
```typescript
263+
const doc = await client.open({
264+
collaboration: {
265+
providerType: 'hocuspocus',
266+
url: 'ws://localhost:1234',
267+
documentId: 'my-doc-room',
268+
},
269+
});
270+
```
271+
</Tab>
272+
<Tab title="Python">
273+
```python
274+
doc = await client.open({
275+
"collaboration": {
276+
"providerType": "hocuspocus",
277+
"url": "ws://localhost:1234",
278+
"documentId": "my-doc-room",
279+
}
280+
})
281+
```
282+
</Tab>
283+
</Tabs>
284+
251285
### Start an empty room from a local `.docx`
252286
253287
If the room is empty, pass `doc` together with collaboration params:
@@ -285,7 +319,7 @@ What happens when you pass `doc`:
285319
286320
| Parameter | Type | Default | Description |
287321
| --- | --- | --- | --- |
288-
| `collabUrl` | `string` | — | WebSocket URL for your collaboration provider. |
322+
| `collabUrl` | `string` | — | WebSocket URL for your collaboration provider. Shorthand defaults to `y-websocket`. |
289323
| `collabDocumentId` | `string` | session ID | Room/document ID on the provider. |
290324
| `doc` | `string` | — | Local `.docx` used only when the room is empty. |
291325
| `onMissing` | `string` | `seedFromDoc` | `seedFromDoc`, `blank`, or `error`. |

packages/sdk/langs/python/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ Use this when your app already has a live collaboration room (Liveblocks, Hocusp
153153

154154
Pass `collabUrl` and `collabDocumentId` to `client.open`:
155155

156+
> The `collabUrl` + `collabDocumentId` shorthand defaults to the `y-websocket` provider. To connect to a Hocuspocus server, pass an explicit `collaboration` object with `providerType: "hocuspocus"`.
157+
156158
```python
157159
import asyncio
158160

@@ -175,6 +177,20 @@ async def main():
175177
asyncio.run(main())
176178
```
177179

180+
### Connect to Hocuspocus explicitly
181+
182+
Use the explicit `collaboration` object when your server speaks the Hocuspocus protocol instead of `y-websocket`:
183+
184+
```python
185+
doc = await client.open({
186+
"collaboration": {
187+
"providerType": "hocuspocus",
188+
"url": "ws://localhost:1234",
189+
"documentId": "my-doc-room",
190+
}
191+
})
192+
```
193+
178194
### Start an empty room from a local `.docx`
179195

180196
If the room is empty, pass `doc` together with collaboration params:
@@ -199,7 +215,7 @@ What happens when you pass `doc`:
199215

200216
| Parameter | Type | Default | Description |
201217
| --- | --- | --- | --- |
202-
| `collabUrl` | `string` || WebSocket URL for your collaboration provider. |
218+
| `collabUrl` | `string` || WebSocket URL for your collaboration provider. Shorthand defaults to `y-websocket`. |
203219
| `collabDocumentId` | `string` | session ID | Room/document ID on the provider. |
204220
| `doc` | `string` || Local `.docx` used only when the room is empty. |
205221
| `onMissing` | `string` | `seedFromDoc` | `seedFromDoc`, `blank`, or `error`. |

0 commit comments

Comments
 (0)