y-socket.io
Version:
Socket IO Connector for Yjs (Inspired by y-websocket)
1 lines • 3.38 kB
Source Map (JSON)
{"version":3,"sources":["../../src/server/server.ts"],"sourcesContent":["#!/usr/bin/env node\r\n\r\nimport http from 'http'\r\nimport { Server, Socket } from 'socket.io'\r\n// import { Document } from './document'\r\nimport { YSocketIO } from './y-socket-io'\r\n\r\nconst host = process.env.HOST ?? 'localhost'\r\nconst port = parseInt(`${process.env.PORT ?? 1234}`)\r\n\r\n// Create the http server\r\nconst server = http.createServer((req, res) => {\r\n res.writeHead(200, { 'Content-Type': 'application/json' })\r\n res.end(JSON.stringify({ ok: true }))\r\n})\r\n// Create an io instance\r\nconst io = new Server(server)\r\n\r\n// Create the YSocketIO instance\r\n// NOTE: This uses the socket namespaces that match the regular expression /^\\/yjs\\|.*$/, make sure that when using namespaces\r\n// for other logic, these do not match the regular expression, this could cause unwanted problems.\r\n// TIP: You can export a new instance from another file to manage as singleton and access documents from all app.\r\nconst ysocketio = new YSocketIO(io, {\r\n // authenticate: (handshake) => handshake.auth.token === 'valid-token',\r\n // OR\r\n // authenticate: (handshake) => {\r\n // return new Promise<boolean>(resolve => {\r\n // setTimeout(() => resolve(handshake.auth.token === 'valid-token'), 2000)\r\n // })\r\n // },\r\n\r\n // levelPersistenceDir: './storage-location',\r\n // gcEnabled: true,\r\n})\r\n\r\n// ysocketio.on('document-loaded', (doc: Document) => console.log(`The document ${doc.name} was loaded`))\r\n// ysocketio.on('document-update', (doc: Document, update: Uint8Array) => console.log(`The document ${doc.name} is updated`))\r\n// ysocketio.on('awareness-update', (doc: Document, update: Uint8Array) => console.log(`The awareness of the document ${doc.name} is updated`))\r\n// ysocketio.on('document-destroy', async (doc: Document) => console.log(`The document ${doc.name} is being destroyed`))\r\n// ysocketio.on('all-document-connections-closed', async (doc: Document) => console.log(`All clients of document ${doc.name} are disconected`))\r\n\r\n// Execute initialize method\r\nysocketio.initialize()\r\n\r\n// Handling another socket namespace\r\nio.on('connection', (socket: Socket) => {\r\n console.log(`[connection] Connected with user: ${socket.id}`)\r\n\r\n // You can add another socket logic here...\r\n socket.on('disconnect', () => {\r\n console.log(`[disconnect] Disconnected with user: ${socket.id}`)\r\n })\r\n})\r\n\r\n// Http server listen\r\nserver.listen(port, host, undefined, () => console.log(`Server running on port ${port}`))\r\n"],"mappings":";;;;;;AAEA,OAAOA,OAAU;AACjB,SAAS,UAAAC,SAAsB;AAH/B,IAAAC,GAOMC,KAAOD,IAAA,QAAQ,IAAI,SAAZ,OAAAA,IAAoB,aAPjCA,GAQME,IAAO,SAAS,IAAGF,IAAA,QAAQ,IAAI,SAAZ,OAAAA,IAAoB,MAAM,GAG7CG,IAASC,EAAK,aAAa,CAACC,GAAKC,MAAQ;AAC7C,EAAAA,EAAI,UAAU,KAAK,EAAE,gBAAgB,mBAAmB,CAAC,GACzDA,EAAI,IAAI,KAAK,UAAU,EAAE,IAAI,GAAK,CAAC,CAAC;AACtC,CAAC,GAEKC,IAAK,IAAIC,EAAOL,CAAM,GAMtBM,IAAY,IAAIC,EAAUH,GAAI,CAWpC,CAAC;AASDE,EAAU,WAAW;AAGrBF,EAAG,GAAG,cAAc,CAACI,MAAmB;AACtC,UAAQ,IAAI,qCAAqCA,EAAO,IAAI,GAG5DA,EAAO,GAAG,cAAc,MAAM;AAC5B,YAAQ,IAAI,wCAAwCA,EAAO,IAAI;AAAA,EACjE,CAAC;AACH,CAAC;AAGDR,EAAO,OAAOD,GAAMD,GAAM,QAAW,MAAM,QAAQ,IAAI,0BAA0BC,GAAM,CAAC;","names":["http","Server","_a","host","port","server","http","req","res","io","Server","ysocketio","YSocketIO","socket"]}