Skip to content
Back

Authentication on custom Websocket Server

  • 0
  • 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
- Developer wants to implement authentication on a custom Websocket Server using Bun - Session cookies are not accessible on localhost, causing issues with authentication - Solution: Implement a custom authentication mechanism within the `fetch` function by checking headers or using tokens for authentication before upgrading the connection.
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