Web3.py: How to print received messages right away using watch(callback) or request get_new_entries() on shh_filter?

Created on 25 Jun 2018  路  3Comments  路  Source: ethereum/web3.py

  • Web3.py version: 4.2.1
  • Python: 3.5.2
  • OS: linux
  • geth version: 1.8.0-unstable
  • I am running geth with --shh flag and --rpcapi "admin,eth,net,web3,debug,shh"

I have followed docs. My simple goal is the print received messages via using watch().

Returns ShhFilter which you can either watch(callback) or request get_new_entries()
$ web3.shh.newMessageFilter({'topic': '0x12340000', 'privateKeyID': recipient_private})
ShhFilter({'filter_id': 'b37c3106cfb683e8f01b5019342399e0d1d74e9160f69b27625faba7a6738554'})

=> Could you please provide a simple example to watch() or request get_new_entries() for the ShhFilter? I just want new received messages to printed on the screen.

I was able to create a filter but it does not print the received messages right away, I need to manually call it such as: print(received_messages[0]).

Example approach on web3.js is:

> web3.shh.newMessageFilter(
        {privateKeyID:kId}, 
        function(err, res) {console.log(web3.toUtf8(res.payload))});

This is how far I get:

from web3 import Web3, HTTPProvider
web3 = Web3(HTTPProvider('http://localhost:8545'))
from web3.shh import Shh
Shh.attach(web3, "shh")

print(web3.shh.info)
pKey = '0xa82ff6abcf75393084edb06d100e53268c5e118884d59282ef9d3396c8537011'; #web3.shh.newKeyPair();
kId = web3.shh.addPrivateKey(pKey)

print(web3.shh.hasKeyPair(kId))
print('PubKey: ' + web3.shh.getPublicKey(kId))

myFilter = web3.shh.newMessageFilter({'topic': '0x07678231', 'privateKeyID': kId})

myFilter.poll_interval = 500;
print(myFilter.poll_interval) 
print(myFilter.filter_id) # Here I obtain the filter-id

received_messages = [];
myFilter.watch(received_messages.extend)  # This stores the coming messages in received_messages but it does not print on screen when new message is received.# <TimerClass(Thread-4, started daemon 139988713903872)>

Thank you for your valuable help and guide.

Most helpful comment

We removed the watch method from the log objects. For some reason it is still lingering in the Shh filter. Have a look at doc for the rationale and some examples for implementing your own asynchronous polling: http://web3py.readthedocs.io/en/latest/filters.html#examples-listening-for-events

All 3 comments

Replace received_messages.extend with a callback function that does everthing you want with new messages. ie:

received_messages = []
def print_message(received_message):
    print(received_message)
    received_messages.append(received_message)

myFilter.watch(print_message)

My simple example filter:

   myFilter = eBlocBroker.events.LogJob.createFilter(
       fromBlock=857060,
       argument_filters={'clusterAddress': '0x4e4a0750350796164D8DefC442a712B7557BF282'}
   )
   received_messages = []
   myFilter.watch(print_message)

=> I am having following error: AttributeError: #'LogFilter' object has no attribute 'watch'.

I guess Watch callback return value exists on Web3.js but it does no exist on Web3.py. @dylanjw

We removed the watch method from the log objects. For some reason it is still lingering in the Shh filter. Have a look at doc for the rationale and some examples for implementing your own asynchronous polling: http://web3py.readthedocs.io/en/latest/filters.html#examples-listening-for-events

Was this page helpful?
0 / 5 - 0 ratings