Skip to content

Instantly share code, notes, and snippets.

@JosephScript
Created March 8, 2024 16:46
Show Gist options
  • Save JosephScript/7bf87cc450d53f474a542f5335a38d65 to your computer and use it in GitHub Desktop.
Save JosephScript/7bf87cc450d53f474a542f5335a38d65 to your computer and use it in GitHub Desktop.
fastify-hocuspocus
import { Server } from '@hocuspocus/server'
import Fastify from 'fastify'
import WebSocketPlugin from 'fastify-websocket'
const port = Number(process.env.PORT || 8800)
const host = '0.0.0.0'
// Initialize Fastify
const fastify = Fastify()
// Register the WebSocket plugin
fastify.register(WebSocketPlugin)
// Configure Hocuspocus server
const server = Server.configure({
port,
name: 'fastify-hocuspocus',
quiet: false,
})
// HTTP route for health checks
fastify.get('/health', (_req, res) => {
const uptime = process.uptime()
res.send({ uptime })
})
// WebSocket route
fastify.get('/collaboration', { websocket: true }, (connection, req) => {
// Handle the WebSocket connection with Hocuspocus
server.handleConnection(connection.socket, req.raw)
})
// Start the server
fastify.listen(port, host, (err) => {
if (err) {
throw err
}
console.info(`Server listening on port ${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment