Whisper Protocol is not well documented related to its usage. I believe this issue will help other users who might face with similar problem.
Each sent message has time-to-live in seconds (ttl
), that is defined by the sender. For more detail please see Whisper v5 RPC API => shh_post. Whisper Protocol
keeps received messages in the memory until messages' time-to-live
time is over, when geth
was closed or shh.newMessageFilter()
is not activated.
=> How could we access and print those received messages that are actually stored in the memory?
shh_getFilterMessages
Retrieve messages that match the filter criteria and are received between the last time this function was called and now.
- But there is no
getFilterMessages()
method with the latestshh
. Please see.
On this solution; we can use same key pairs after geth
is restarted. But the issue after geth
is restarted and later when we run shh.newMessageFilter()
; it does not retrieve/return any message that was sent while geth
was closed.
[Q] How could we obtain/filter messages that sent when geth
was closed or shh.newMessageFilter()
is not activated?. When we run web3.shh.newMessageFilter()
, it only retrieves/filters messages right after it start running and it does not filter/print any message before it starts running.
Example Scenario:
=> On the receiver node I closed geth
and from the sender node, I have sent multiple messages into receiver node. Later, when I reopened geth
on the receiver node, shh.info.memory
verifies that there are received messages in memory, but I was not able to retrieve/filter those messages using web3.shh.newMessageFilter()
.
> shh.info.memory
780
Here is some logs:
Creating web3.shh.newMessageFilter()
:
> kId=shh.addPrivateKey('0x8de0b39f3a4a109f27bb06a92dab680d58e8f029eaca61da6be7115981a2ed11')
"439bfd6aa63a81b404ba20e4fc250e11c326d72ad9943c72dc445f476638989d"
> filter=web3.shh.newMessageFilter(
... {privateKeyID:kId},
... function(err, res) {console.log(web3.toUtf8(res.payload))});
{
callbacks: [function(err, res)],
filterId: "a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78",
formatter: null,
getLogsCallbacks: [],
implementation: {
getLogs: function(),
newFilter: function(),
poll: function(),
uninstallFilter: function()
},
options: {
privateKeyID: "439bfd6aa63a81b404ba20e4fc250e11c326d72ad9943c72dc445f476638989d"
},
pollFilters: [],
requestManager: {
polls: {
82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597: {
data: {...},
id: "82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597",
callback: function(error, messages),
uninstall: function()
},
a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78: {
data: {...},
id: "a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78",
callback: function(error, messages),
uninstall: function()
}
},
provider: {
newAccount: function(),
openWallet: function(),
send: function github.com/ethereum/go-ethereum/console.(*bridge).Send-fm(),
sendAsync: function github.com/ethereum/go-ethereum/console.(*bridge).Send-fm(),
sign: function(),
unlockAccount: function()
},
timeout: {},
poll: function(),
reset: function(keepIsSyncing),
send: function(data),
sendAsync: function(data, callback),
sendBatch: function(data, callback),
setProvider: function(p),
startPolling: function(data, pollId, callback, uninstall),
stopPolling: function(pollId)
},
get: function(callback),
stopWatching: function(callback),
watch: function(callback)
}
I can see shh_getFilterMessages
under filter.requestManager.polls
but I cannot call it.
> filter.requestManager.polls
{
82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597: {
data: {
method: "shh_getFilterMessages",
params: ["82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597"]
},
id: "82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597",
callback: function(error, messages),
uninstall: function()
},
a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78: {
data: {
method: "shh_getFilterMessages",
params: ["a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78"]
},
id: "a0b4194651c9143d977c2fa4848ab681b414720364461b4096fb17588d84ae78",
callback: function(error, messages),
uninstall: function()
}
}
> a.requestManager.polls.82f08d2d7fe974a69acdadcfac39f788575d337eed9577969b617ae28462e597
(anonymous): Line 1:23 Unexpected token ILLEGAL
web3.py
approach:
from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")
import time
print(web3.shh.info)
keyPair = web3.shh.newKeyPair();
kId = web3.shh.addPrivateKey(keyPair)
print(web3.shh.hasKeyPair(kId))
print('PubKey: ' + web3.shh.getPublicKey(kId))
#Message sent before filter is assigned
web3.shh.post({
'pubKey': web3.shh.getPublicKey(kId),
'topic': '0x07678231',
'powTarget': 2.01,
'powTime': 2,
'ttl': 10,
'payload': web3.toHex(text="test message :)")
});
time.sleep(1)
received_messages = [];
myFilter = web3.shh.newMessageFilter({'topic': '0x07678231',
'privateKeyID': kId
});
myFilter.poll_interval = 600;
print(myFilter.filter_id)
received_messages = myFilter.get_all_entries() # Error occurs!!
print(received_messages)
Error I am having:
File "/home/alper/eBlocBroker/venv/lib/python3.5/site-packages/web3/utils/filters.py", line 191, in get_all_entries
raise NotImplementedError()
NotImplementedError