Created
June 19, 2025 13:58
-
-
Save aonemd/b75f4649c9405e9756714c7fae97606a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import PusherSwift | |
class PusherManager: ObservableObject, PusherDelegate { | |
@Published var messages: [String] = [] | |
var pusher: Pusher! | |
init() { | |
let options = PusherClientOptions( | |
autoReconnect: true, | |
host: .cluster("eu") // !!! change cluster here | |
) | |
pusher = Pusher( | |
key: "APP_KEY", // !!! change app key here | |
options: options | |
) | |
pusher.delegate = self | |
let channel = pusher.subscribe("my-channel") | |
let _ = channel.bind(eventName: "my-event") { (event: PusherEvent) in | |
if let data = event.data { | |
print("Received message: \(data)") | |
self.addMessage("Received message: \(data)") | |
} | |
} | |
pusher.connect() | |
addMessage("Connecting to Pusher...") | |
} | |
// PusherDelegate methods | |
func changedConnectionState(from old: ConnectionState, to new: ConnectionState) { | |
DispatchQueue.main.async { | |
self.addMessage("Connection state: \(old) -> \(new)") | |
} | |
} | |
func subscribedToChannel(name: String) { | |
DispatchQueue.main.async { | |
self.addMessage("Subscribed to channel: \(name)") | |
} | |
} | |
func receivedError(error: PusherError) { | |
DispatchQueue.main.async { | |
self.addMessage("Error: \(error.message)") | |
} | |
} | |
private func addMessage(_ message: String) { | |
let timestamp = DateFormatter.localizedString(from: Date(), dateStyle: .none, timeStyle: .medium) | |
messages.append("[\(timestamp)] \(message)") | |
} | |
} | |
struct ContentView: View { | |
@StateObject private var pusherManager = PusherManager() | |
var body: some View { | |
VStack(spacing: 20) { | |
Text("Simple Pusher Echo") | |
.font(.largeTitle) | |
.fontWeight(.bold) | |
Text("Listening for 'my-event' on 'my-channel'") | |
.font(.headline) | |
.foregroundColor(.secondary) | |
ScrollView { | |
LazyVStack(alignment: .leading, spacing: 2) { | |
ForEach(Array(pusherManager.messages.enumerated()), id: \.offset) { index, message in | |
Text(message) | |
.font(.system(.caption, design: .monospaced)) | |
.textSelection(.enabled) | |
.frame(maxWidth: .infinity, alignment: .leading) | |
} | |
} | |
} | |
.frame(height: 400) | |
.background(Color.gray.opacity(0.1)) | |
.cornerRadius(8) | |
Text("Send 'my-event' to 'my-channel' from Pusher Debug Console") | |
.font(.caption) | |
.foregroundColor(.secondary) | |
Spacer() | |
} | |
.padding() | |
.frame(minWidth: 500, minHeight: 600) | |
} | |
} | |
@main | |
struct PusherTesterApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment