diff --git a/Whisper-Push-Notifications.md b/Whisper-Push-Notifications.md index 4dedcc8..8093efa 100644 --- a/Whisper-Push-Notifications.md +++ b/Whisper-Push-Notifications.md @@ -62,6 +62,9 @@ var newChatSessionTopic = '0x509579a2'; // NEW_CHAT_SESSION var ackNewChatSessionTopic = '0xd012aae8'; // ACK_NEW_CHAT_SESSION var newDeviceRegistrationTopic = '0x14621a51'; // NEW_DEVICE_REGISTRATION var ackDeviceRegistrationTopic = '0x424358d6'; // ACK_DEVICE_REGISTRATION +var checkClientSessionTopic = '0x8745d931'; // CHECK_CLIENT_SESSION +var confirmClientSessionTopic = '0xd3202c5f'; // CONFIRM_CLIENT_SESSION +var dropClientSessionTopic = '0x3a6656bb'; // DROP_CLIENT_SESSION ``` Finally, in order to test everything out, you'd better use two different clients not one. So, assume that we have two nodes (`Device A` and `Device B`) which have different underlying Status daemons: @@ -390,6 +393,65 @@ We provide all the code for `Device B` in a single chunk. Basically, all that `D ### 4. Checking subscription status and unsubscribing +```js +var checkClientSessionTopic = '0x8745d931'; // CHECK_CLIENT_SESSION +var confirmClientSessionTopic = '0xd3202c5f'; // CONFIRM_CLIENT_SESSION +var dropClientSessionTopic = '0x3a6656bb'; // DROP_CLIENT_SESSION +var removeClientSession = function () { + var checkClientSession = function (callback) { + // before sending request, let's start waiting for response + var filter = web3.shh.filter({ + to: identity, + topics: [confirmClientSessionTopic] + }); + filter.watch(function (error, result) { + if (!error) { + // response will be in JSON, e.g. {"server": "0xdeadbeef", "key": "0xdeadbeef"} + var payload = JSON.parse(web3.toAscii(result.payload)); + console.log("Client session confirmation received: ", result, payload); + + // no need to watch for the filter any more + filter.stopWatching(); + + callback(payload) + } + }); + + // send enquiry to all servers, to learn whether we have a client session with any of them + var err = web3.shh.post({ + from: identity, + to: protocolKey, + topics: [checkClientSessionTopic], + ttl: 20 + }); + if (err !== null) { + console.log("message NOT sent") + } else { + console.log("message sent OK") + } + }; + + checkClientSession(function (payload) { + console.log("time to cleanup: ", payload.server); + console.log("session key: ", payload.key); + + setTimeout(function () { + // notify server that you want to unsubscribe + var err = web3.shh.post({ + from: identity, + to: protocolKey, + topics: [dropClientSessionTopic], + ttl: 20 + }); + if (err !== null) { + console.log("message NOT sent") + } else { + console.log("message sent OK") + } + }, 5000); // let's all other concurrent tasks to wrap up + }); +}; +``` # Complete Sample