pusher / pusher-websocket-swift

Pusher Channels websocket library for Swift
https://pusher.com/channels
MIT License
273 stars 167 forks source link

Swift Combine Publisher support #403

Closed johnnewman closed 1 year ago

johnnewman commented 1 year ago

Description of the pull request

Hello! This pull request adds Combine Publisher support for Pusher events. This is done via an extension on the Pusher class that allows you to subscribe to channel events and global events. Below are some code examples of how this could be used.

Channel events:

let pusher = Pusher(key: "APP_KEY")
...
pusher
    .publisher(channelName: "my-channel", eventName: "my-event")
    .sink { event in
        // Do something with the PusherEvent. 
    }
    .store(in: &cancellables)

All global events:

pusher
    .publisher()
    .sink { event in
        // Do something with the global PusherEvent. 
    }
    .store(in: &cancellables)

Specific global events:

pusher
    .publisher(eventName: "my-event")
    .sink { event in
        // Do something with the global PusherEvent. 
    }
    .store(in: &cancellables)

Here's an example of a more advanced use case:

pusher
    .publisher(channelName: "my-channel", eventName: "my-event")
    .throttle(for: .milliseconds(500), scheduler: DispatchQueue.main, latest: true)
    .compactMap { $0.dataToJSONObject() as? [AnyHashable: Any] }
    .sink { myJSONObject in
        // do something
    }
    .store(in: &cancellables)

Why is the change necessary?

This provides an alternate approach to receiving events and is a convenient way of integrating Pusher into Combine streams.

johnnewman commented 1 year ago

👋 is there anything you need from me in order to merge this PR? Thank you!

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.