supabase / supabase-swift

A Swift client for Supabase

Home Page:https://supabase.com/docs/reference/swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2.3.1 realtimev2 connection throwing `bad response from the server`

paulsumit1993 opened this issue · comments

Bug report

Updated the library to 2.3.1 and followed the migration guide.

When I try it connect, I get the following log in succession:

Error Domain=NSURLErrorDomain Code=-1011 "There was a bad response from the server.
wss://<supbase-project-id>.supabase.co/realtime/v1/websocket?vsn=2.0.0&Authorization=Bearer%20ey...

The code in question

final class SupabaseService {
    let client: SupabaseClient
    
    init() {
        self.client = supabase
        client.realtime.connect()
    }
    
    deinit {
        
    }
    
    func subscribe(for messageId: Int, conversationId: Int?) async -> AsyncStream<SupabaseMessage> {
        let (stream, continuation) = AsyncStream<SupabaseMessage>.makeStream()
        
        let channel = await client.realtimeV2.channel("public")
        
        let insertions = await channel.postgresChange(InsertAction.self, table: "messages")
        let updates = await channel.postgresChange(UpdateAction.self, table: "messages")
        
        await channel.subscribe()
        
        for await insertion in insertions {
            do {
                let decodedMessage = try insertion.decodeRecord(as: RealtimeMessageV2.self, decoder: decoder)
                if let message = SupabaseMessage(message: decodedMessage.rawMessage,
                                                 messageId: messageId,
                                                 conversationId: conversationId) {
                    print("[Supabase] - \(messageId): \(message.text)")
                    continuation.yield(message)
                }
            } catch {
                print(error)
            }
        }
        
        for await update in updates {
            do {
                let decodedMessage = try update.decodeRecord(as: RealtimeMessageV2.self, decoder: decoder)
                if let message = SupabaseMessage(message: decodedMessage.rawMessage,
                                                 messageId: messageId,
                                                 conversationId: conversationId) {
                    print("[Supabase] - \(messageId): \(message.text)")
                    continuation.yield(message)
                }
            } catch {
                print(error)
            }
        }
        return stream
    }
}

Any help in resolving this would be greatly appreciated, thank you!

Hi @paulsumit1993

The realtime you're connecting on the init is the old realtime object, not the realtimeV2. But this doesn't matter as once you call channel.subscribe() it automatically connects the channel.

  • The error message may be from the realtime.connect() call, try removing that line.
  • Also, noticed an issue with your code, you have to wrap each of the for await inside a Task, otherwise the first for await will block execution until it finishes.
func subscribe(for messageId: Int, conversationId: Int?) async -> AsyncStream<SupabaseMessage> {
  let (stream, continuation) = AsyncStream<SupabaseMessage>.makeStream()

  let channel = await client.realtimeV2.channel("public")

  let insertions = await channel.postgresChange(InsertAction.self, table: "messages")
  let updates = await channel.postgresChange(UpdateAction.self, table: "messages")

  await channel.subscribe()

  Task {
    for await insertion in insertions {
      do {
        let decodedMessage = try insertion.decodeRecord(
          as: RealtimeMessageV2.self, decoder: decoder)
        if let message = SupabaseMessage(
          message: decodedMessage.rawMessage,
          messageId: messageId,
          conversationId: conversationId)
        {
          print("[Supabase] - \(messageId): \(message.text)")
          continuation.yield(message)
        }
      } catch {
        print(error)
      }
    }
  }

  Task {
    for await update in updates {
      do {
        let decodedMessage = try update.decodeRecord(as: RealtimeMessageV2.self, decoder: decoder)
        if let message = SupabaseMessage(
          message: decodedMessage.rawMessage,
          messageId: messageId,
          conversationId: conversationId)
        {
          print("[Supabase] - \(messageId): \(message.text)")
          continuation.yield(message)
        }
      } catch {
        print(error)
      }
    }
  }
  return stream
}

Try the above items, and let me know if you still face any issues.

@grdsdev Thank you for your help, after applying the fixes, now I'm getting a new error

Connection not set before response is received, failing task
Task <DBFA57F9-1A51-414E-9262-E190DF088F86>.<1> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."