I am trying to connect to a specific channel so that I can only listen events emitted to that channel. I have used Laravel Echo on server side.
I am trying to connect to a specific channel so that I can only listen events emitted to that channel. I have used Laravel Echo on server side.
Finally after spending some time on this issue I got the solution. Here is the solution
private let manager = SocketManager(socketURL: URL(string: "http://testserver.com:6001")!, config: [.log(true), .compress, .reconnects(true), .reconnectAttempts(-1), .forceWebsockets(true), .forcePolling(true)])
private var socket: SocketIOClient!
private var nameSpace = "App\\Events\\"
func connect() {
socket = manager.defaultSocket
socket.connect()
socket.on(clientEvent: .connect) {data, ack in
let channelData = ["channel": "test-channel"]
self.socket.emit("subscribe", channelData) {
print("Sockets: test-channel subscribed")
socket.on("\(nameSpace)newMessage") { (data, ack) in
print("Sockets: newMessage event called")
}
}
}
}
I would look into URLSessionWebSocketTask.. it's a bit weird how they set up the API but it's the only option in the standard library. I added a link below to get started..
https://medium.com/better-programming/websockets-in-ios-13-using-swift-and-xcode-11-18fa3000d802
As for the channel subscription, you'll most likely need to send messages through the socket and saving a reference of that channel on the backend. This is what the frontend code might look like..
https://github.com/BJBeecher/Networking/blob/master/Sources/Networking/WebSockets/WebSocket.swift