I have noticed there are a couple projects out there using y-websockets v1 due to it being the only version that exports the setupWSConnection function. Lexical text editor playground example from meta is one such example. The have some decent documentation implementing yjs to allow for easy setup of user collaboration. It would be nice if future versions would allow importing of the function as it is very difficult to implement a clean way to use y-websocket within nestjs without it.
Here is an example:
import {
WebSocketGateway,
WebSocketServer,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Request } from 'express';
import { Server } from 'ws';
import { setupWSConnection } from 'y-websocket/bin/utils'; // this is y-websocket v1
import { setupWSConnection } from 'y-websocket-server' // something like this would help
import { getCookie } from './utils';
@WebSocketGateway({ path: '/yjs' })
export class YjsGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor() {}
@WebSocketServer()
server: Server;
handleConnection(connection: WebSocket, request: Request): void {
const docName = getCookie(request?.headers?.cookie, 'roomName');
setupWSConnection(connection, request, { ...(docName && { docName }) });
}
handleDisconnect(): void {}
}
I have noticed there are a couple projects out there using y-websockets v1 due to it being the only version that exports the setupWSConnection function. Lexical text editor playground example from meta is one such example. The have some decent documentation implementing yjs to allow for easy setup of user collaboration. It would be nice if future versions would allow importing of the function as it is very difficult to implement a clean way to use y-websocket within nestjs without it.
Here is an example:
import {
WebSocketGateway,
WebSocketServer,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Request } from 'express';
import { Server } from 'ws';
import { setupWSConnection } from 'y-websocket/bin/utils'; // this is y-websocket v1
import { setupWSConnection } from 'y-websocket-server' // something like this would help
import { getCookie } from './utils';
@WebSocketGateway({ path: '/yjs' })
export class YjsGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor() {}
@WebSocketServer()
server: Server;
handleConnection(connection: WebSocket, request: Request): void {
const docName = getCookie(request?.headers?.cookie, 'roomName');
setupWSConnection(connection, request, { ...(docName && { docName }) });
}
handleDisconnect(): void {}
}