Search code examples
swiftserver-sent-eventsfacebook-commentsfacebook-live-api

How to listen to Server Side events for Facebook live comments on IOS


I'm using the new SSE endpoints for Live comments on Live videos. my question is how can I achive this on IOS and what library should I use

I tried using the Event Source for IOS https://github.com/inaka/EventSource

But Even though I tried to use OnMessage and addOnEventsource it dosent seem to work.

Here is my code for listening to the live comments

    var live_commentURL =  URL.init(string:"https://streaming-graph.facebook.com/\(fbLiveStreamId!)/live_comments?access_token=\(accessToken ?? "")&comment_rate=one_per_two_seconds&fields=from{name,id},message")


        let queryItems = [NSURLQueryItem(name: "access_token", value: accessToken!), NSURLQueryItem(name: "comment_rate", value: "one_hundred_per_second"),NSURLQueryItem(name: "fields", value: "from{name,id},message")]
        let urlComps = NSURLComponents(string: "https://streaming-graph.facebook.com/\(fbLiveStreamId!)/live_comments")!
//        var headders:[String:String] = [:]
//        headders["access_token"] = accessToken!
//        headders["comment_rate"] = "one_hundred_per_second"
//        headders["fields"] = "from{name,id},message"

        var eventSource = EventSource.init(url:urlComps.url!)
        eventSource.connect()
        eventSource.onOpen {
            print("Successfully Connected to server")
        }
        eventSource.addEventListener("user-connected") {(id, event, data) in
            print("id:", id)
            print("event:" , event)
            print("data: ", data)
        }

        eventSource.onMessage { (id, event, data) in
            print("id:", id)
            print("event:" , event)
            print("data: ", data)
        }
        print(eventSource.events())
        eventSource.onComplete { (code, isErro, error) in
            print(code)
        }

I have tried to send the access tokesn and other fields in headders too but got no luck with it.

I tried 2 methods

method 1 send accesstoken,comment_rateand fields as headders but I dont think that is the right way.

method 2

Since they are all query params I used NSURLComponents.


Solution

  • It was an issue with my auth tokens My bad