edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

[Supabase Edge Functions] Can't use Websockets in Edge Functions on Supabase Hosting.

Open Lan-Hekary opened this issue 1 year ago • 6 comments

Bug report

  • [x] I confirm this is a bug with Supabase, not with my own application.
  • [x] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I am trying to connect to Supabase Edge Function via WebSocket. I managed to connect to the Edge Function locally and even when I expose the local instance of the Function to the internet I can connect to it from outside. But as soon as I Deploy the function to a Supabase Project The WebSocket Connection fails. I am not sure if it's a Bug or it's not supported.

To Reproduce

  1. Create a Supabase function using the CLI: Supabase functions new ws

  2. Use This code is based on this article from Deno:

Deno.serve((req) => {
  console.log("got :", req)
  const upgrade = req.headers.get("upgrade") || ""
  if (upgrade.toLowerCase() != "websocket") {
    return new Response("request isn't trying to upgrade to websocket.")
  }
  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, e)
  socket.onclose = (e) => console.log("socket closed clean:", e.wasClean)
  return response
})
  1. Up to this Step if you started the function using supabase functions serve --no-verify-jwt and opened a WebSocket connection to the local instance of the edge-runtime, it will work just fine. The Bug is in the Next Step.

  2. Deploy the Function using CLI: supabase functions deploy ws --no-verify-jwt.

  3. Try to connect to the function via the Webbrowser or using https://websocketking.com/, it will fail as it can't connect, but in the logs of the function you will findout that the socket opens and immediately errors out then closes with an error message 'Unexpected EOF'

Expected behavior

It Should work in Supabase Projects as it's working in the local runtime.

Screenshots

The local CLI instance Working Properly. image

The log in the Dashboard showing the failure. image

System information

  • OS: Linux Arm64
  • Browser: Edge
  • Version of supabase-cli: v1.157.2
  • Version of Node.js: v18.20.2

Additional context

My Reference is this Official Video which referenced this article from Deno.

I Duplicated the Video for the Server-Side-Events, and it works as Expected both locally and on Supabase Projects. The video talks about WebSockets briefly but does not implement it.

Lan-Hekary avatar Apr 16 '24 15:04 Lan-Hekary

Hi @Lan-Hekary

Yes, it is currently not supported in Supabase cloud as Websockets are not enabled on the underlying provider. Will change this to an enhancement and @laktek can comment more if any progress is made

encima avatar Apr 22 '24 12:04 encima

This would be a hugely appreciated enhancement 🙏🏻

geoffreygarrett avatar Apr 23 '24 15:04 geoffreygarrett

I agree this feature would be awesome. Hey @laktek, do you have any estimation for it? I'd like to use this feature to implement this feature Sadly, I posted this like a year ago and no one noticed. So, I am trying to implement it myself.

Lan-Hekary avatar Apr 24 '24 18:04 Lan-Hekary

I was pretty surprised to discover this wouldn't work - lifetime aside, I think this would be a great place to put a lightweight sync server (that also has option value to save state back to pgsql if needed).

+1 for prioritizing!

jamesgpearce avatar Jul 26 '24 02:07 jamesgpearce

Moving to edge-runtime to help the team track it as a feature request.

encima avatar Jul 29 '24 11:07 encima

Given the new realtime API from OpenAI that relies on websockets , are there any plans to implement this feature in the foreseeable future?

haschu avatar Oct 02 '24 11:10 haschu

Hey, just a friendly reminder :)

This is labeled as "enhancement" while it's actually a bug, there is even an example on how to use websockets in this repo, which results in the same error.

WebSocket support was introduced with this PR by @nyannyacha.

haschu avatar Oct 19 '24 17:10 haschu

Hello @haschu 😋

I checked the example you mentioned, but WebSocket seems to be working fine locally. Can you provide reproducible steps?

Note that currently, this feature is not available in Edge Functions (aka, Supabase Hosted), only self-hosted users deploying the edge runtime in their own environment can use it.

nyannyacha avatar Oct 20 '24 00:10 nyannyacha

@nyannyacha Thanks for your reply 🙃 sorry... my bad, I thought it should be working on Supabase hosted as well. Are there any plans for this feature in the near future?

haschu avatar Oct 21 '24 08:10 haschu

Hi Supabase,

Hi Supabase. Are there any updates on this front? I second @haschu thoughts regarding openAi realtime. That's exactly my use case.

Can you suggest other deno-edge providers which allow the feature? (You've mentioned above it's your provider that causes the issue).

BrandiATMuhkuh avatar Nov 29 '24 07:11 BrandiATMuhkuh

We are closing this issue as we now support WebSocket in Supabase Edge Functions (hosted environment). Please see the latest guide. https://supabase.com/docs/guides/functions/websockets

nyannyacha avatar Dec 03 '24 23:12 nyannyacha

Awesome work! ❤️

haschu avatar Dec 04 '24 08:12 haschu