Created
March 8, 2024 16:46
-
-
Save JosephScript/7bf87cc450d53f474a542f5335a38d65 to your computer and use it in GitHub Desktop.
fastify-hocuspocus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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