Skip to main content

Handling WebSockets

还没翻译译自英文(supabase.com)本页 24 段都还没译

Edge Functions supports hosting WebSocket servers that can facilitate bi-directional communications with browser clients.

This allows you to:

  • Build real-time applications like chat or live updates
  • Create WebSocket relay servers for external APIs
  • Establish both incoming and outgoing WebSocket connections

For a production-ready reconnect pattern with session persistence and replay, see Resumable WebSockets with Edge Functions.


Creating WebSocket servers

Here are some basic examples of setting up WebSocket servers using Deno and Node.js APIs.

DenoNode.js

export default {
  fetch: (req) => {
    const upgrade = req.headers.get('upgrade') || ''

    if (upgrade.toLowerCase() != 'websocket') {
      return Response.json(
        { error: "request isn't trying to upgrade to WebSocket." },
        { status: 400 }
      )
    }

    const { socket, response } = Deno.upgradeWebSocket(req)

    socket.onopen = () => console.log('socket opened')
    socket.onmessage = (e) => {
      console.log('socket message:', e.data)
      socket.send(new Date().toString())
    }

    socket.onerror = (e) => console.log('socket errored:', e.message)
    socket.onclose = () => console.log('socket closed')

    return response
  },
}

Outbound WebSockets

You can also establish an outbound WebSocket connection to another server from an Edge Function.

Combining it with incoming WebSocket servers, it's possible to use Edge Functions as a WebSocket proxy, for example as a relay server for the OpenAI Realtime API.

import { createServer } from "node:http";
import { WebSocketServer } from "npm:ws";
import { RealtimeClient } from "https://raw.githubusercontent.com/openai/openai-realtime-api-beta/refs/heads/main/lib/client.js";

// ...

const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");

const server = createServer();
// Since we manually created the HTTP server,
// turn on the noServer mode.
const wss = new WebSocketServer({ noServer: true });

wss.on("connection", async (ws) => {
  console.log("socket opened");
  if (!OPENAI_API_KEY) {
    throw new Error("OPENAI_API_KEY is not set");
  }
  // Instantiate new client
  console.log(`Connecting with key "${OPENAI_API_KEY.slice(0, 3)}..."`);
  const client = new RealtimeClient({ apiKey: OPENAI_API_KEY });

  // Relay: OpenAI Realtime API Event -> Browser Event
  client.realtime.on("server.*", (event) => {
    console.log(`Relaying "${event.type}" to Client`);
    ws.send(JSON.stringify(event));
  });
  client.realtime.on("close", () => ws.close());

  // Relay: Browser Event -> OpenAI Realtime API Event
  // We need to queue data waiting for the OpenAI connection
  const messageQueue = [];
  const messageHandler = (data) => {
    try {
      const event = JSON.parse(data);
      console.log(`Relaying "${event.type}" to OpenAI`);
      client.realtime.send(event.type, event);
    } catch (e) {
      console.error(e.message);
      console.log(`Error parsing event from client: ${data}`);
    }
  };

  ws.on("message", (data) => {
    if (!client.isConnected()) {
      messageQueue.push(data);
    } else {
      messageHandler(data);
    }
  });
  ws.on("close", () => client.disconnect());

  // Connect to OpenAI Realtime API
  try {
    console.log(`Connecting to OpenAI...`);
    await client.connect();
  } catch (e) {
    console.log(`Error connecting to OpenAI: ${e.message}`);
    ws.close();
    return;
  }
  console.log(`Connected to OpenAI successfully!`);
  while (messageQueue.length) {
    messageHandler(messageQueue.shift());
  }
});

server.on("upgrade", (req, socket, head) => {
  wss.handleUpgrade(req, socket, head, (ws) => {
    wss.emit("connection", ws, req);
  });
});

server.listen(8080);

Authentication

WebSocket browser clients don't have the option to send custom headers. Because of this, Edge Functions won't be able to perform the usual authorization header check to verify the JWT.

You can skip the default authorization header checks by explicitly providing --no-verify-jwt when serving and deploying functions.

To authenticate the user making WebSocket requests, you can pass the JWT in URL query params or via a custom protocol. The withSupabase wrapper validates credentials on request headers, so it can't authenticate WebSocket clients. Verify the JWT yourself, as shown below.

Using query paramsUsing custom protocol

import { createClient } from 'npm:@supabase/supabase-js@^2'

const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)
const supabase = createClient(
  Deno.env.get('SUPABASE_URL'),
  // If you want to use a different api key, change 'default' to your preferred key name
  SUPABASE_SECRET_KEYS['default']
)

export default {
  fetch: async (req) => {
    const upgrade = req.headers.get('upgrade') || ''
    if (upgrade.toLowerCase() != 'websocket') {
      return Response.json(
        { error: "request isn't trying to upgrade to WebSocket." },
        { status: 400 }
      )
    }

    // Please be aware query params may be logged in some logging systems.
    const url = new URL(req.url)
    const jwt = url.searchParams.get('jwt')

    if (!jwt) {
      console.error('Auth token not provided')
      return Response.json({ error: 'Auth token not provided' }, { status: 403 })
    }

    const { error, data } = await supabase.auth.getUser(jwt)

    if (error) {
      console.error(error)
      return Response.json({ error: 'Invalid token provided' }, { status: 403 })
    }

    if (!data.user) {
      console.error('user is not authenticated')
      return Response.json({ error: 'User is not authenticated' }, { status: 403 })
    }

    const { socket, response } = Deno.upgradeWebSocket(req)

    socket.onopen = () => console.log('socket opened')
    socket.onmessage = (e) => {
      console.log('socket message:', e.data)
      socket.send(new Date().toString())
    }

    socket.onerror = (e) => console.log('socket errored:', e.message)
    socket.onclose = () => console.log('socket closed')

    return response
  },
}

Caution

The maximum duration is capped based on the wall-clock, CPU, and memory limits. The Function will shutdown when it reaches one of these limits.

When using WebSockets, keep in mind that the HTTP request is considered complete after Deno.upgradeWebSocket(req) returns the response. To prevent early worker retirement while the socket is still open, keep an unresolved EdgeRuntime.waitUntil() promise that resolves in socket.onclose.


Testing WebSockets locally

When testing Edge Functions locally with Supabase CLI, the instances are terminated automatically after a request is completed. This will prevent keeping WebSocket connections open.

To prevent that, you can update the supabase/config.toml with the following settings:

[edge_runtime]
policy = "per_worker"

Caution

When running with per_worker policy, Function won't auto-reload on edits. You will need to manually restart it by running supabase functions serve.