-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
48 lines (39 loc) · 2.14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
import * as http from 'http'
import { Server, Socket } from 'socket.io'
import { YSocketIO } from 'y-socket.io/dist/server';
const host = process.env.HOST ?? 'localhost'
const port = parseInt(`${process.env.PORT ?? 1234}`)
// Create the http server
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ ok: true }))
})
// Create an io instance
const io = new Server(server)
// Create the YSocketIO instance
// NOTE: This uses the socket namespaces that match the regular expression /^\/yjs\|.*$/, make sure that when using namespaces
// for other logic, these do not match the regular expression, this could cause unwanted problems.
// TIP: You can export a new instance from another file to manage as singleton and access documents from all app.
const ysocketio = new YSocketIO(io, {
// authenticate: (auth) => auth.token === 'valid-token',
// levelPersistenceDir: './storage-location',
// gcEnabled: true,
})
// ysocketio.on('document-loaded', (doc: Document) => console.log(`The document ${doc.name} was loaded`))
// ysocketio.on('document-update', (doc: Document, update: Uint8Array) => console.log(`The document ${doc.name} is updated`))
// ysocketio.on('awareness-update', (doc: Document, update: Uint8Array) => console.log(`The awareness of the document ${doc.name} is updated`))
// ysocketio.on('document-destroy', async (doc: Document) => console.log(`The document ${doc.name} is being destroyed`))
// ysocketio.on('all-document-connections-closed', async (doc: Document) => console.log(`All clients of document ${doc.name} are disconected`))
// Execute initialize method
ysocketio.initialize()
// Handling another socket namespace
io.on('connection', (socket: Socket) => {
console.log(`[connection] Connected with user: ${socket.id}`)
// You can add another socket logic here...
socket.on('disconnect', () => {
console.log(`[disconnect] Disconnected with user: ${socket.id}`)
})
})
// Http server listen
server.listen(port, host, undefined, () => console.log(`Server running on port ${port}`))