open_upstream_socket multiple notifications bug #5

Closed
sidbatra opened this Issue Feb 18, 2011 · 7 comments

Comments

Projects
None yet
2 participants
@sidbatra

On using an open upstream socket to send multiple notifications as follows (using my own tokens)

# Initialize some notifications
notification1 = APND::Notification.new(
:alert  => 'Alert!',
:token  => 'fe15a27d5df3c34778defb1f4f3880265cc52c0c047682223be59fb68500a9a2',
:badge  => 1
)

notification2 = APND::Notification.new(
  :alert  => 'Red Alert!',
  :token  => '55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4',
  :badge  => 99
)

# Send multiple notifications at once to avoid overhead in
# opening/closing the upstream socket connection each time
APND::Notification.open_upstream_socket do |sock|
 sock.write(notification1.to_bytes)
 sock.write(notification2.to_bytes)
end

The apnd log shows the notification being sent to the first device id twice

[2011-02-17 23:46:41] Queue has 2 items
[2011-02-17 23:46:42] Sending notification for  fe15a27d5df3c34778defb1f4f3880265cc52c0c047682223be59fb68500a9a2
[2011-02-17 23:46:42] Sending notification for    fe15a27d5df3c34778defb1f4f3880265cc52c0c047682223be59fb68500a9a2

I think this may be a bug but please let me know if there is something wrong with syntax. Thanks.

@itspriddle

This comment has been minimized.

Show comment Hide comment
@itspriddle

itspriddle Feb 18, 2011

Owner

There was a problem in an older version in which multiple notifications weren't being parsed properly (see fcdac4).

Try installing 0.1.7 and using sock.puts instead of sock.write (that appends a '\n' character). The protocol class splits incoming streams from the socket on '\n'. So what's happening is instead of receiving 'notification1bytes\nnotification2bytes\n...' it receives 'notification1bytesnotification2bytes...', and only the first notification is recognized. Sorry, I had forgotten all about this when I replied to your issue yesterday.

Let me know if that works for you and I'll push another update.

Here's a sample from a rails app I have in production:

devices = Device.where('push_enabled = 1')
if devices.size > 0
  socket = APND::Notification.open_upstream_socket do |sock|
    devices.each do |device|
      notification = APND::Notification.new(
        :token => device.push_token,
        :badge => 0,
        :alert => "Alert",
        :sound => 'default'
      )
      sock.puts(notification.to_bytes)
    end
  end
end
Owner

itspriddle commented Feb 18, 2011

There was a problem in an older version in which multiple notifications weren't being parsed properly (see fcdac4).

Try installing 0.1.7 and using sock.puts instead of sock.write (that appends a '\n' character). The protocol class splits incoming streams from the socket on '\n'. So what's happening is instead of receiving 'notification1bytes\nnotification2bytes\n...' it receives 'notification1bytesnotification2bytes...', and only the first notification is recognized. Sorry, I had forgotten all about this when I replied to your issue yesterday.

Let me know if that works for you and I'll push another update.

Here's a sample from a rails app I have in production:

devices = Device.where('push_enabled = 1')
if devices.size > 0
  socket = APND::Notification.open_upstream_socket do |sock|
    devices.each do |device|
      notification = APND::Notification.new(
        :token => device.push_token,
        :badge => 0,
        :alert => "Alert",
        :sound => 'default'
      )
      sock.puts(notification.to_bytes)
    end
  end
end
@sidbatra

This comment has been minimized.

Show comment Hide comment
@sidbatra

sidbatra Feb 18, 2011

Thanks. There is a tiny issue - I upgraded to 0.1.7 and this is the code I am using. Notification tokens are printed for testing.

    socket = APND::Notification.open_upstream_socket do |sock|
      users.each do |user|
        notification = APND::Notification.new(
                              :token  => user.iphone_device_id,
                              :badge  => user.unread_count,
                              :sound => "default"
                              )
        puts notification.token
        sock.puts(notification.to_bytes)
      end
    end

This is the output on the console

55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

and this is the output in apnd.log

[2011-02-18 02:03:30] 127.0.0.1:54958 opened connection
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 closed connection
[2011-02-18 02:03:33] Queue has 6 items
[2011-02-18 02:03:33] Sending notification for     55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

Perhaps an error with parsing? Thanks for your help.

Thanks. There is a tiny issue - I upgraded to 0.1.7 and this is the code I am using. Notification tokens are printed for testing.

    socket = APND::Notification.open_upstream_socket do |sock|
      users.each do |user|
        notification = APND::Notification.new(
                              :token  => user.iphone_device_id,
                              :badge  => user.unread_count,
                              :sound => "default"
                              )
        puts notification.token
        sock.puts(notification.to_bytes)
      end
    end

This is the output on the console

55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

and this is the output in apnd.log

[2011-02-18 02:03:30] 127.0.0.1:54958 opened connection
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 added new Notification to queue
[2011-02-18 02:03:30] 127.0.0.1:54958 closed connection
[2011-02-18 02:03:33] Queue has 6 items
[2011-02-18 02:03:33] Sending notification for     55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f
[2011-02-18 02:03:33] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:03:33] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

Perhaps an error with parsing? Thanks for your help.

@sidbatra

This comment has been minimized.

Show comment Hide comment
@sidbatra

sidbatra Feb 18, 2011

OK, so I added an extra "\n" in the sock.puts

    socket = APND::Notification.open_upstream_socket do |sock|
      users.each do |user|
        notification = APND::Notification.new(
                              :token  => user.iphone_device_id,
                              :badge  => user.unread_followings_count + 1,
                              :sound => "default"
                              )
        puts notification.token
        sock.puts(notification.to_bytes + "\n")
      end
    end

and now its down to 3 notifications from the 6 earlier.

[2011-02-18 02:21:05] 127.0.0.1:44462 opened connection
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 closed connection
[2011-02-18 02:21:08] Queue has 3 items
[2011-02-18 02:21:08] Sending notification for    55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:21:08] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:21:08] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

Hope this helps in debugging

OK, so I added an extra "\n" in the sock.puts

    socket = APND::Notification.open_upstream_socket do |sock|
      users.each do |user|
        notification = APND::Notification.new(
                              :token  => user.iphone_device_id,
                              :badge  => user.unread_followings_count + 1,
                              :sound => "default"
                              )
        puts notification.token
        sock.puts(notification.to_bytes + "\n")
      end
    end

and now its down to 3 notifications from the 6 earlier.

[2011-02-18 02:21:05] 127.0.0.1:44462 opened connection
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 added new Notification to queue
[2011-02-18 02:21:05] 127.0.0.1:44462 closed connection
[2011-02-18 02:21:08] Queue has 3 items
[2011-02-18 02:21:08] Sending notification for    55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:21:08] Sending notification for 55711f0b3493d48abe829fe0f94f0829086335418e96d041ed62c5c7681904b4
[2011-02-18 02:21:08] Sending notification for cd4d003ebaa9e38b40728cf4b425f8b0d8aebde105c52ec5f2b9b550eb0cd93f

Hope this helps in debugging

@itspriddle

This comment has been minimized.

Show comment Hide comment
@itspriddle

itspriddle Feb 18, 2011

Owner

I think I've isolated it to the way APND::Daemon::Protocol was buffering incoming data.

Can you try this and let me know if it works? I'll push the new version to rubygems if it does.

git clone git://github.com/itspriddle/apnd.git
cd apnd
gem build apnd.gemspec
gem install apnd-0.1.8.gem
Owner

itspriddle commented Feb 18, 2011

I think I've isolated it to the way APND::Daemon::Protocol was buffering incoming data.

Can you try this and let me know if it works? I'll push the new version to rubygems if it does.

git clone git://github.com/itspriddle/apnd.git
cd apnd
gem build apnd.gemspec
gem install apnd-0.1.8.gem
@sidbatra

This comment has been minimized.

Show comment Hide comment
@sidbatra

sidbatra Feb 18, 2011

Confirmed, it works. With the code below. Looking forward to version 0.1.8

Thanks for all your help.

socket = APND::Notification.open_upstream_socket do |sock|
  users.each do |user|
    notification = APND::Notification.new(
                          :token  => user.iphone_device_id,
                          :badge  => user.unread_count,
                          :sound => "default"
                          )
    puts notification.token
    sock.puts(notification.to_bytes + "\n")
  end
end

Confirmed, it works. With the code below. Looking forward to version 0.1.8

Thanks for all your help.

socket = APND::Notification.open_upstream_socket do |sock|
  users.each do |user|
    notification = APND::Notification.new(
                          :token  => user.iphone_device_id,
                          :badge  => user.unread_count,
                          :sound => "default"
                          )
    puts notification.token
    sock.puts(notification.to_bytes + "\n")
  end
end
@itspriddle

This comment has been minimized.

Show comment Hide comment
@itspriddle

itspriddle Feb 18, 2011

Owner

0.1.8 is up on rubygems.org now. Thanks again for your help fixing that bug.

Owner

itspriddle commented Feb 18, 2011

0.1.8 is up on rubygems.org now. Thanks again for your help fixing that bug.

@sidbatra

This comment has been minimized.

Show comment Hide comment
@sidbatra

sidbatra Feb 18, 2011

Splendid!

Splendid!

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment