nomad-cli / houston

Apple Push Notifications; No Dirigible Required
http://nomad-cli.com
MIT License
2.93k stars 229 forks source link

Persistent by default #25

Closed mlangenberg closed 11 years ago

mlangenberg commented 11 years ago

When you hold a Houston::Client, calling #push should be responsible for opening and closing the connection.

Apple advices to use persistent connections, and this should be the default for any library talking to the APN-service. This pull request is a little rough, but it demonstrates how one could use Houston for a persistent connection to apple, while still making use of the error handling code in Houston::Client.

Before

connection = Houston::Connection.new(APPLE_DEVELOPMENT_GATEWAY_URI, certificate, passphrase)
connection.open

notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
connection.write(notification.message)
# connection.read(6) or what?

After

client = Houston::Client.development certificate: certificate, passphrase: "..."
notification = Houston::Notification.new(device: token)
notification.alert = "Hello, World!"
client.push(notification) # error handling, converts notification to byte string for you.

Also started on some tests.

mattt commented 11 years ago

Thanks for all of your contributions, @mlangenberg. Some thoughts about this particular PR:

Houston is designed the way it is to accommodate both basic and advanced use cases simply. If you're just sending a few notifications occasionally, Houston::Client makes it easy, managing the connection for you.

Advanced usage, such as scheduling with a queueing service or background worker means managing the Houston::Connection object manually. This level of abstraction is acceptable for the advanced use case, and is indeed desirable, as less or indirect control over this would limit the range of how people might use it.

The problem with making Houston::Client a persistent connection by default is that I don't expect these objects to be long-lived, like a managed connection might be.