Cannot Connect Client with StreamIO

124 Views Asked by At

I'm following the latest tutorial from Stream Chat, which looks great.

Looks like I followed it to the letter except for I replaced the apiKey with one created for me in the dashboard. This was provided when I registered my free trial.

Unfortunately, I'm unable to connect.

Here's my code

SwiftUIChatDemo

import SwiftUI

// 1 Add imports
import StreamChat
import StreamChatSwiftUI

@main
struct SwiftUIChatDemoApp: App {
    
    // 2 Add Adapter
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ChatChannelListView()
        }
    }
}

App Delegate

import StreamChat
import StreamChatSwiftUI
import UIKit
import SwiftUI

class AppDelegate: NSObject, UIApplicationDelegate {
    
    // Add context provider
    
    var streamChat: StreamChat?
    
    var chatClient: ChatClient = {
        // Low-level client variable with a hard-coded apikey
        var config = ChatClientConfig(apiKey: .init("[key I created in dashboard]"))
        
        // Set to use the chat in offline mode
        config.isLocalStorageEnabled = true
        
        // Pass the low-level client variable as a parameter of the ChatClient
        let client = ChatClient(config: config)
        return client
    }()
    
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions:
                     [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        // Initialize the stream chat client
        streamChat = StreamChat(chatClient: chatClient)
        
        connectUser()
        
        return true
    }
    
    // The `connectUser` function we need to add.
    private func connectUser() {
        // This is a hardcoded token valid on Stream's tutorial environment.
        let token = try! Token(rawValue: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoibHVrZV9za3l3YWxrZXIifQ.kFSLHRB5X62t0Zlc7nwczWUfsQMwfkpylC6jCUZ6Mc0")
        
        // Call `connectUser` on our SDK to get started.
        chatClient.connectUser(
            userInfo: .init(id: "luke_skywalker",
                            name: "Luke Skywalker",
                            imageURL: URL(string: "https://vignette.wikia.nocookie.net/starwars/images/2/20/LukeTLJ.jpg")!),
            token: token
        ) { error in
            if let error = error {
                // Some very basic error handling only logging the error.
                log.error("connecting the user failed \(error)")
                return
            }
        }
    }
}

SwiftUIChatDemo.app

import SwiftUI

// 1 Add imports
import StreamChat
import StreamChatSwiftUI

@main
struct SwiftUIChatDemoApp: App {
    
    // 2 Add Adapter
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    var body: some Scene {
        WindowGroup {
            ChatChannelListView()
        }
    }
}
2

There are 2 best solutions below

1
On

When you change the apiKey, you will need to generate new tokens with the secret that's provided to you in the dashboard. For development, you can generate tokens with the tool here: https://getstream.io/chat/docs/ios-swift/tokens_and_authentication/?language=swift#manually-generating-tokens. The user id also needs to match to your user. Note that for production usage, you should generate this tokens on a backend. In case of additional questions, you can also reach our support channel: https://getstream.io/contact/support/

0
On

UPDATE

So, I got it working with the apiKey and token values from the tutorial. Well done. How I do this with my own values probably requires my own users.