$30 off During Our Annual Pro Sale. View Details »

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. Hatena
    Goes
    Realtime
    Kansai.pm #14
    2011-11-26 (Sat.)
    @Hatena Kyoto

    View Slide

  2. Realtime Web
    Push Notification
    Hatena::Notify
    APNs in Hatena

    View Slide

  3. Kentaro Kuribayashi
    id:antipop @kentaro
    http://kentaro.hatenablog.com/

    View Slide

  4. PrePAN
    Social reviewing site for Perl Modules
    @YAPC::Asia 2011
    Application Engineer
    Working for Hatena, Inc.
    Media Artist
    To be popular with girls...

    View Slide

  5. Perl
    Emacs
    Kyoto
    Momoclo
    Architecture Jazz
    Sake
    Chinese Tea
    Utsuwa
    Cooking
    Arduino
    Gastronomade
    Nabokov
    Media Art
    Girls

    View Slide

  6. Realtime Web

    View Slide

  7. Rapid
    Communication to be...
    Social
    Frequent Intimate
    More Looser

    View Slide

  8. Twitter
    Facebook
    Hatena
    Google+

    View Slide

  9. Laptops
    Smartphones
    Tablets

    View Slide

  10. Push Notification

    View Slide

  11. iPhone
    Android
    APNs: Apple Push Notification Service
    C2DM: Cloud to Device Messaging

    View Slide

  12. iPhone
    APNs: Apple Push Notification Service

    View Slide

  13. iOS4 to iOS5
    • Alert → Banner
    • Doesn’t disturb
    interaction
    • Notification Center

    View Slide

  14. View Slide

  15. APNs in
    3 Minutes

    View Slide

  16. •Provider (Web Service)
    •APNs
    •Device (iPhone)
    •Client App (Application)

    View Slide

  17. 1. Device requests token
    2. APNs gives it
    3. Device (Client App)register it to
    Provider

    View Slide

  18. 1. Provider sends
    notification with
    token
    2. APNs verifies it and
    push notification to
    Device
    3. Client App handles it

    View Slide

  19. That’s All!

    View Slide

  20. What you must implement:
    Client App
    Retrieves and register device token
    Provider
    Sends notification using the token

    View Slide

  21. Hatena::Notify

    View Slide

  22. Hatena::Notify

    View Slide

  23. User adds ˒ to some entry

    Notification will be appear
    on “Global Header”

    View Slide

  24. User comments on some entry

    Notification will be appear
    on “Global Header”

    View Slide

  25. User starts following someone

    Notification will be appear
    on “Global Header”

    View Slide

  26. Notification Center
    Hatena::Star
    Hatena::Blog
    UgoMemo::Hatena Hatena::Bookmark
    Hatena::Diary etc.

    View Slide

  27. Applications just send
    notifications on each events
    Hatena::Notify is a hub of
    Hatena services

    View Slide

  28. APNs in Hatena

    View Slide

  29. Web Services
    Device
    +

    View Slide

  30. ίϨφχ
    Questions
    with
    Images

    View Slide

  31. View Slide

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

    View Slide

  33. TheSchwartz
    with
    WorkerManager

    View Slide

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

    View Slide

  35. AnyEvent::MPRPC
    AnyEvent::APNS
    with
    daemontools

    View Slide

  36. 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

    View Slide

  37. 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

    View Slide

  38. 1. Opens socket to APNs
    2. Writes notification data
    into it
    3. Independent binary format

    View Slide

  39. • Device Token
    • JSON-formatted Hash
    • aps (reserved)
    • alert / badge / sound
    • arbitrary keys

    View Slide

  40. {
    aps => {
    alert => 'Something occurred!',
    badge => 1,
    sound => 'notification.wav',
    },
    type => 'comment',
    foo => 'bar'
    }

    View Slide

  41. 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

    View Slide

  42. $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

    View Slide

  43. $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

    View Slide

  44. • RPC using AE::MPRPC
    • Connection plooling
    • Some custom fields
    • subject
    •verb

    View Slide

  45. • Notification cooperating with Web
    service
    • Scalable and stable system
    architecture
    • Combination of existing modules
    • Hold connections and make them
    persistent
    Recap

    View Slide

  46. Any Questions?

    View Slide