Skip to content

Commit

Permalink
Merge pull request #176 from inaka/ferigis.175.add_chatterbox
Browse files Browse the repository at this point in the history
[#175] replace gun by chatterbox
  • Loading branch information
cabol authored May 29, 2017
2 parents 11e0eda + 9010854 commit f24eb6b
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 233 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,29 @@ We can use this token for an entire hour, after that we will receive something l

## Reconnection

If network goes down or something unexpected happens the `gun` connection with APNs will go down. In that case `apns4erl` will send a message `{reconnecting, ServerPid}` to the client process, that means `apns4erl` lost the connection and it is trying to reconnect. Once the connection has been recover a `{connection_up, ServerPid}` message will be send.

If something unexpected happens and the `chatterbox` connection with APNs crashes `apns4erl` will send a message `{reconnecting, ServerPid}` to the client process, that means `apns4erl` lost the connection and it is trying to reconnect. Once the connection has been recover a `{connection_up, ServerPid}` message will be send.

We implemented an *Exponential Backoff* strategy. We can set the *ceiling* time adding the `backoff_ceiling` variable on the `config` file. By default it is set to 10 (seconds).

## Timeout

When we call `apns:push_notification/3,4` or `apns:push_notification_token/4,5` we could get a `timeout` that could be caused if the network went down. Here is the `timeout` format:

```erlang
{timeout, stream_id()}
```
where that `stream_id()` is an identifier for the notification.


Getting a `timeout` doesn't mean your notification to APNs is lost. If `apns4erl` connects to network again it will try to send your notification, in that case `apns4erl` will send back a message to the client with the format:

```erlang
{apns_response, ServerPid, StreamID, Response}
```
where that StreamId should match with the `stream_id` we got on the `timeout` tuple.

You should check your client inbox after a timeout but it is not guaranteed your message was send successfully.

## Close connections

Apple recommends us to keep our connections open and avoid opening and closing very often. You can check the [Best Practices for Managing Connections](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html) section.
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
%% == Dependencies ==

{deps, [
{gun, {git, "https://github.com/ninenines/gun.git", {ref, "bc733a2ca5f7d07f997ad6edf184f775b23434aa"}}},
{chatterbox, "0.4.2"},
{jsx, "2.8.1"},
{base64url, "0.0.1"}
]}.
Expand Down
22 changes: 9 additions & 13 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
{"1.1.0",
[{<<"base64url">>,{pkg,<<"base64url">>,<<"0.0.1">>},0},
{<<"cowlib">>,
{git,"https://github.com/ninenines/cowlib",
{ref,"07cde7c6def6eeed0174b270229e7d5175673d87"}},
1},
{<<"gun">>,
{git,"https://github.com/ninenines/gun.git",
{ref,"bc733a2ca5f7d07f997ad6edf184f775b23434aa"}},
0},
{<<"chatterbox">>,{pkg,<<"chatterbox">>,<<"0.4.2">>},0},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},2},
{<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},1},
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.8.1">>},0},
{<<"ranch">>,
{git,"https://github.com/ninenines/ranch",
{ref,"a004ad710eddd0c21aaccc30d5633a76b06164b5"}},
1}]}.
{<<"lager">>,{pkg,<<"lager">>,<<"3.2.4">>},1}]}.
[
{pkg_hash,[
{<<"base64url">>, <<"36A90125F5948E3AFD7BE97662A1504B934DD5DAC78451CA6E9ABF85A10286BE">>},
{<<"jsx">>, <<"1453B4EB3615ACB3E2CD0A105D27E6761E2ED2E501AC0B390F5BBEC497669846">>}]}
{<<"chatterbox">>, <<"BA68296FA79F0CA31139713C688C350A26C8844E1244F1736D7845C1C72E5F87">>},
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
{<<"hpack">>, <<"17670F83FF984AE6CD74B1C456EDDE906D27FF013740EE4D9EFAA4F1BF999633">>},
{<<"jsx">>, <<"1453B4EB3615ACB3E2CD0A105D27E6761E2ED2E501AC0B390F5BBEC497669846">>},
{<<"lager">>, <<"A6DEB74DAE7927F46BD13255268308EF03EB206EC784A94EAF7C1C0F3B811615">>}]}
].
2 changes: 1 addition & 1 deletion src/apns.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
kernel,
stdlib,
jsx,
gun,
chatterbox,
base64url
]},
{modules, []},
Expand Down
10 changes: 5 additions & 5 deletions src/apns.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,16 @@
, response/0
, token/0
, headers/0
, stream_id/0
]).

-type json() :: #{binary() => binary() | json()}.
-type json() :: #{binary() | atom() => binary() | json()}.
-type device_id() :: binary().
-type stream_id() :: integer().
-type response() :: { integer() % HTTP2 Code
, [term()] % Response Headers
, [term()] | no_body % Response Body
} | timeout.
} | {timeout, stream_id()}.
-type token() :: binary().
-type headers() :: #{ apns_id => binary()
, apns_expiration => binary()
Expand Down Expand Up @@ -84,9 +86,7 @@ connect(Type, ConnectionName) ->
%% @doc Connects to APNs service
-spec connect(apns_connection:connection()) -> {ok, pid()} | {error, timeout}.
connect(Connection) ->
{ok, _} = apns_sup:create_connection(Connection),
Server = whereis(apns_connection:name(Connection)),
apns_connection:wait_apns_connection_up(Server).
apns_sup:create_connection(Connection).

%% @doc Closes the connection with APNs service.
-spec close_connection(apns_connection:name()) -> ok.
Expand Down
Loading

0 comments on commit f24eb6b

Please sign in to comment.