Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Hatena Goes Realtime #kansaipm 14

Hatena Goes Realtime #kansaipm 14

How does Hatena do with so-called Realtime-Web, especially about Hatena::Notify and APNs.

Kentaro Kuribayashi

November 26, 2011
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

  1. PrePAN Social reviewing site for Perl Modules @YAPC::Asia 2011 Application

    Engineer Working for Hatena, Inc. Media Artist To be popular with girls...
  2. Perl Emacs Kyoto Momoclo Architecture Jazz Sake Chinese Tea Utsuwa

    Cooking Arduino Gastronomade Nabokov Media Art Girls
  3. iOS4 to iOS5 • Alert → Banner • Doesn’t disturb

    interaction • Notification Center
  4. 1. Device requests token 2. APNs gives it 3. Device

    (Client App)register it to Provider
  5. 1. Provider sends notification with token 2. APNs verifies it

    and push notification to Device 3. Client App handles it
  6. What you must implement: Client App Retrieves and register device

    token Provider Sends notification using the token
  7. 1. Services 2. Notification Center 3. Job Queue + Workers

    4. Daemon (apnsd) 5. APNs 6. iPhone
  8. 1. Services 2. Notification Center 3. Job Queue + Workers

    4. Daemon (apnsd) 5. APNs 6. iPhone
  9. use AnyEvent::MPRPC; # server my $server = mprpc_server '127.0.0.1', '4423';

    $server->reg_cb( notify => sub { my ($res_cv, $params) = @_; # ... do something ... $res_cv->result($result); }, ); # client my $client = mprpc_client '127.0.0.1', '4423'; $client->call(notify => $params)->recv; AnyEvent::MPRPC
  10. use AnyEvent::APNS; my $cv = AnyEvent->condvar; my $apns; $apns =

    AnyEvent::APNS->new( certificate => 'your apns certificate file', private_key => 'your apns private key file', on_error => sub { ... }, on_connect => sub { $apns->send($device_token => { aps => { alert => 'Message received from Bob', }, }); }, ); AnyEvent::APNS
  11. • Device Token • JSON-formatted Hash • aps (reserved) •

    alert / badge / sound • arbitrary keys
  12. { aps => { alert => 'Something occurred!', badge =>

    1, sound => 'notification.wav', }, type => 'comment', foo => 'bar' }
  13. To be sure to make connection persistent APNs may consider

    connections that are rapidly and repeatedly established and torn down as a denial-of-service attack. Upon error, APNs closes the connection on which the error occurred. https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/ CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
  14. $self->client->call(notify => { app_name => $self->app_name, device_token => pack('H*', $self-

    >device_token), payload => { aps => { alert => $message, badge => $badge, sound => $sound, }, subject => $self->subject || '', verb => $self->verb || '', }, })->recv; AnyEvent::MPRPC::Client
  15. $self->server->reg_cb( notify => sub { my ($res_cv, $params) = @_;

    $params ||= {}; my $app_name = $params->{app_name}; my $device_token = $params->{device_token}; my $payload = $params->{payload}; my $connection = $self->connection($app_name); $res_cv->result( $connection->notify($device_token, $payload) ); }, ); AnyEvent::MPRPC::Server
  16. • Notification cooperating with Web service • Scalable and stable

    system architecture • Combination of existing modules • Hold connections and make them persistent Recap