How to use y-protocol with typescript

Implementing a websocket-server on which a y-websocket provider can rely requires using lib0 and y-protocols.

y-protocols exposes esm modules (i’m not sure if that’s the right term) for awareness and sync with type-definition, while the example y-websocket server uses the y-protocols/dist/*.cjs libs. The esm modules give error when I try to run the code compiled with typescript

The y-protocols/dist/*.cjs libs have no type definitions, which makes it impossible to use them in a typescript setup (and I cannot give up typescript).

How can I get started with building a typescript websockets server that is horizontally scalable, persists y-js documents, and which y-websocket can connect to as a provider while maintaining proper typescript typings?

Instead of importing lib0/encoding.js, you can import lib0/encoding. This should give you typings and automatic module resolution to the cjs files if you are running a current node version.

  • upgrade to node >=v14.7
  • use lib0/encoding instead of lib0/encoding.js

I am already doing that, you can check the code over here https://github.com/kapv89/crdt-backend/blob/main/src/service.ts

The code compiles well and good. But when I try to run the file https://github.com/kapv89/crdt-backend/blob/main/src/app.ts, I get the following errors: https://paste.laravel.io/42e6e5a6-aea7-4ac0-88e0-76ddbe0e9427

My node version is 14.16.0

Please help

I think I have things working now. Had to convert everything to use esModules and then deal with the technicalities that come with that. Thanks