{"id":15316325,"url":"https://github.com/miapolis/y-websocket","last_synced_at":"2025-10-09T02:31:26.212Z","repository":{"id":48520639,"uuid":"516929434","full_name":"miapolis/y-websocket","owner":"miapolis","description":"Websocket Connector for Yjs","archived":false,"fork":true,"pushed_at":"2022-08-08T12:23:07.000Z","size":224,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-27T00:31:44.610Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.yjs.dev/ecosystem/connection-provider/y-websocket","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"yjs/y-websocket","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miapolis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-23T01:33:22.000Z","updated_at":"2022-07-23T01:35:35.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/miapolis/y-websocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/miapolis/y-websocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miapolis%2Fy-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miapolis%2Fy-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miapolis%2Fy-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miapolis%2Fy-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miapolis","download_url":"https://codeload.github.com/miapolis/y-websocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miapolis%2Fy-websocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279000780,"owners_count":26082906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T08:53:50.341Z","updated_at":"2025-10-09T02:31:25.895Z","avatar_url":"https://github.com/miapolis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# y-websocket :tophat:\n\u003e WebSocket Provider for Yjs\n\nThe Websocket Provider implements a classical client server model. Clients connect to a single endpoint over Websocket. The server distributes awareness information and document updates among clients.\n\nThe Websocket Provider is a solid choice if you want a central source that handles authentication and authorization. Websockets also send header information and cookies, so you can use existing authentication mechanisms with this server.\n\n* Supports cross-tab communication. When you open the same document in the same browser, changes on the document are exchanged via cross-tab communication ([Broadcast Channel](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) and [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) as fallback).\n* Supports exchange of awareness information (e.g. cursors).\n\n## Quick Start\n\n### Install dependencies\n\n```sh\nnpm i y-websocket\n```\n\n### Start a y-websocket server\n\nThis repository implements a basic server that you can adopt to your specific use-case. [(source code)](./bin/)\n\nStart a y-websocket server:\n\n```sh\nHOST=localhost PORT=1234 npx y-websocket\n```\n\n### Client Code:\n\n```js\nimport * as Y from 'yjs'\nimport { WebsocketProvider } from 'y-websocket'\n\nconst doc = new Y.Doc()\nconst wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-roomname', doc)\n\nwsProvider.on('status', event =\u003e {\n  console.log(event.status) // logs \"connected\" or \"disconnected\"\n})\n```\n\n#### Client Code in Node.js\n\nThe WebSocket provider requires a [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) object to create connection to a server. You can polyfill WebSocket support in Node.js using the [`ws` package](https://www.npmjs.com/package/ws).\n\n```js\nconst wsProvider = new WebsocketProvider('ws://localhost:1234', 'my-roomname', doc, { WebSocketPolyfill: require('ws') })\n```\n\n## API\n\n```js\nimport { WebsocketProvider } from 'y-websocket'\n```\n\n\u003cdl\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider = new WebsocketProvider(serverUrl: string, room: string, ydoc: Y.Doc [, wsOpts: WsOpts])\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eCreate a new websocket-provider instance. As long as this provider, or the connected ydoc, is not destroyed, the changes will be synced to other clients via the connected server. Optionally, you may specify a configuration object. The following default values of wsOpts can be overwritten. \u003c/dd\u003e\n\u003c/dl\u003e\n\n```js\nwsOpts = {\n  // Set this to `false` if you want to connect manually using wsProvider.connect()\n  connect: true,\n  // Specify a query-string that will be url-encoded and attached to the `serverUrl`\n  // I.e. params = { auth: \"bearer\" } will be transformed to \"?auth=bearer\"\n  params: {}, // Object\u003cstring,string\u003e\n  // You may polyill the Websocket object (https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).\n  // E.g. In nodejs, you could specify WebsocketPolyfill = require('ws')\n  WebsocketPolyfill: Websocket,\n  // Specify an existing Awareness instance - see https://github.com/yjs/y-protocols\n  awareness: new awarenessProtocol.Awareness(ydoc),\n  // Specify the maximum amount to wait between reconnects (we use exponential backoff).\n  maxBackoffTime: 2500\n}\n```\n\n\u003cdl\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.wsconnected: boolean\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eTrue if this instance is currently connected to the server.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.wsconnecting: boolean\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eTrue if this instance is currently connecting to the server.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.shouldConnect: boolean\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eIf false, the client will not try to reconnect.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.bcconnected: boolean\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eTrue if this instance is currently communicating to other browser-windows via BroadcastChannel.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.synced: boolean\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eTrue if this instance is currently connected and synced with the server.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.disconnect()\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eDisconnect from the server and don't try to reconnect.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.connect()\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eEstablish a websocket connection to the websocket-server. Call this if you recently disconnected or if you set wsOpts.connect = false.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.destroy()\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eDestroy this wsProvider instance. Disconnects from the server and removes all event handlers.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.on('sync', function(isSynced: boolean))\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eAdd an event listener for the sync event that is fired when the client received content from the server.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.on('status', function({ status: 'disconnected' | 'connecting' | 'connected' }))\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eReceive updates about the current connection status.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.on('connection-close', function(WSClosedEvent))\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eFires when the underlying websocket connection is closed. It forwards the websocket event to this event handler.\u003c/dd\u003e\n  \u003cb\u003e\u003ccode\u003ewsProvider.on('connection-error', function(WSErrorEvent))\u003c/code\u003e\u003c/b\u003e\n  \u003cdd\u003eFires when the underlying websocket connection closes with an error. It forwards the websocket event to this event handler.\u003c/dd\u003e\n\u003c/dl\u003e\n\n## Websocket Server\n\nStart a y-websocket server:\n\n```sh\nHOST=localhost PORT=1234 npx y-websocket\n```\n\nSince npm symlinks the `y-websocket` executable from your local `./node_modules/.bin` folder, you can simply run npx. The `PORT` environment variable already defaults to 1234, and `HOST` defaults to `localhost`.\n\n### Websocket Server with Persistence\n\nPersist document updates in a LevelDB database.\n\nSee [LevelDB Persistence](https://github.com/yjs/y-leveldb) for more info.\n\n```sh\nHOST=localhost PORT=1234 YPERSISTENCE=./dbDir node ./node_modules/y-websocket/bin/server.js\n```\n\n### Websocket Server with HTTP callback\n\nSend a debounced callback to an HTTP server (`POST`) on document update. Note that this implementation doesn't implement a retry logic in case the `CALLBACK_URL` does not work.\n\nCan take the following ENV variables:\n\n* `CALLBACK_URL` : Callback server URL\n* `CALLBACK_DEBOUNCE_WAIT` : Debounce time between callbacks (in ms). Defaults to 2000 ms\n* `CALLBACK_DEBOUNCE_MAXWAIT` : Maximum time to wait before callback. Defaults to 10 seconds\n* `CALLBACK_TIMEOUT` : Timeout for the HTTP call. Defaults to 5 seconds\n* `CALLBACK_OBJECTS` : JSON of shared objects to get data (`'{\"SHARED_OBJECT_NAME\":\"SHARED_OBJECT_TYPE}'`)\n\n```sh\nCALLBACK_URL=http://localhost:3000/ CALLBACK_OBJECTS='{\"prosemirror\":\"XmlFragment\"}' npm start\n```\nThis sends a debounced callback to `localhost:3000` 2 seconds after receiving an update (default `DEBOUNCE_WAIT`) with the data of an XmlFragment named `\"prosemirror\"` in the body.\n\n## License\n\n[The MIT License](./LICENSE) © Kevin Jahns\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiapolis%2Fy-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiapolis%2Fy-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiapolis%2Fy-websocket/lists"}