Skip to content
Back

Authentication on custom Websocket Server

  • 0
  • 3
  • Self Hosted
  • Web
Patrick
15 Mar, 2026, 12:22

Hi,

I want to use a custom Websocket Server (using Bun) for my application. However I cant really figure out authentication on custom servers. Session cookies seem to be HTTP and secure only, which prevents me from reading them in localhost.

How would I implement the authentication here?

TypeScript
Bun.serve({
  port: parseInt(process.env.PORT as string),
  hostname: "localhost",
  development: true,
  async fetch(req, server) {

    // ... authenticate here

    if (server.upgrade(req)) {
      return undefined;
    }

    return new Response("Upgrade failed", { status: 500 });
  },
  websocket: {
    // ...
  },
});
TL;DR
To authenticate on a custom Websocket server, users need to connect and then send authentication information within 60 seconds. Follow a similar method as Discord's WebSocket handling or utilize Appwrite's Server-Side Rendering documentation. Use a custom endpoint corresponding to your backend endpoint. The Appwrite cookie should be sent to Appwrite and your backend for authentication.
17 Mar, 2026, 21:43

the way discord handle websocket is that :

  • the user connects to the websocket
  • then sends a message with the auth infos (e.g. the discord token)
  • if the server receive the auth infos, it checks it and start the handle for the messages
  • if the user does not send the auth message in 60s, the server disconnect the user
23 Mar, 2026, 16:29

follow the same flow for SSR login: https://appwrite.io/docs/products/auth/server-side-rendering.

Make sure you use a custom endpoint that matches your backend endpoint.

Then, the appwrite cookie will be sent to both appwrite and your backend and can be used for auth

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more