3

Im trying to using "AuthRequestBuilderProtocol" for authenticating the Pusher connection. But it's not called, I create a class AuthRequestBuilder that inherit from AuthRequestBuilderProtocol like the code below

class AuthRequestBuilder: AuthRequestBuilderProtocol {
func requestFor(socketID: String, channelName: String) -> URLRequest? {
    print("HELLO FROM AUTH REQUEST BUILDER!")
    let request = NSMutableURLRequest(url: URL(string: "https://xxxxxxxxxx/broadcasting/auth")!)
    request.httpMethod = "POST"
    request.httpBody = "socket_id=\(socketID)&channel_name=\(channelName)".data(using: String.Encoding.utf8)
    print("socketID: \(socketID), ChannelName: \(channelName)")
    request.addValue("Bearer " + UserDefaults.standard.retreiveUserAccessToken(), forHTTPHeaderField: "Authorization")
    return request as URLRequest
}

}

In the main controller i define the following variables

var client: Pusher?
var channel: PusherChannel?
var options: PusherClientOptions?
var appKey: String = "xxxx"

And in viewDidLoad i define the following:

    override func viewDidLoad() {
    super.viewDidLoad()
    options = PusherClientOptions(
        authMethod: AuthMethod.authRequestBuilder(authRequestBuilder: AuthRequestBuilder()),
        autoReconnect: true, host: .host("xxxxxxxxxx"),
        port: 6001,
        path: "/",
        activityTimeout: 10
    )
    client = Pusher(withAppKey: appKey, options: options!)
    client?.connection.delegate = self
    channel = client?.subscribeToPresenceChannel(channelName: "presence-Room.\(idOfRooms ?? 0)")
    client?.connect()
    print(channel?.name ?? "")
    print(channel?.subscribed ?? "")
    print(channel?.type ?? "")
    print(channel?.unsentEvents ?? "")
    
    // bind to all events globally
    let _ = client?.bind(eventCallback: { (event: PusherEvent) in
        var message = "Received event: '\(event.eventName)'"
        
        if let channel = event.channelName {
            message += " on channel '\(channel)'"
        }
        if let userId = event.userId {
            message += " from user '\(userId)'"
        }
        if let data = event.data {
            message += " with data '\(data)'"
        }
        
        print(message)
    })}

The problem the AuthRequestBuilder not called when i called in viewDidLoad so i have a problem to subscribe to presence channel, any idea how to fix the problem

Ghassan
  • 60
  • 1
  • 11
  • Does the delegate define any error callbacks? If yes, what do they show? Does the return value of bind need to be retained for it to receive events? Does Pusher have a debug option that you can set and then examine the error logs? – schmittsfn Aug 12 '21 at 22:44

1 Answers1

1

All the method that i provided above is correct! The problem is the backend server version not compatible with the latest version of PusherSwift so i downgrade from 10.0.0 to 8.0.0

Ghassan
  • 60
  • 1
  • 11