Hooks are a core concept to control the behaviour and data flow of the server. All hooks are async. That means you can even do things like executing API requests, running DB queries, trigger webhooks or whatever you need to do to integrate it into your application.

If a user isn’t allowed to connect: Just send reject() in the onConnect() hook. Nice, isn’t it?

Available hooks

Hook Description Link
beforeHandleMessage Before handling a message Read more
onConnect When a connection is established Read more
connected After a connection has been establied Read more
onAuthenticate When authentication is passed Read more
onAwarenessUpdate When awareness changed Read more
onLoadDocument When a new document is created Read more
onChange When a document has changed Read more
onDisconnect When a connection was closed Read more
onListen When the serer is intialized Read more
onDestroy When the server will be destroyed Read more
onConfigure When the server has been configured Read more
onRequest When a HTTP request comes in Read more
onStoreDocument When a document has been changed Read more
onUpgrade When the WebSocket connection is upgraded Read more


import { Server } from '@hocuspocus/server'

const server = Server.configure({
  async onAuthenticate({ documentName, token }) {

    // Could be an API call, DB query or whatever …
    // The endpoint should return 200 OK in case the user is authenticated, and an http error
    // in case the user is not.
    return axios.get('/user', {
      headers: {
        Authorization: `Bearer ${token}`