I have been trying to figure out how to interact with whisper using web3. I have been able to successfully send (and receive) a message using geth console, but have been unable to do the same using web3.js.

Would you provide a short JavaScript snippet that successfully sends and receives a whisper message?

  • - what provider you are using? (web3.setProvider)? - what was the error when you were using web3.js? – Miao ZhiCheng Dec 3 '17 at 17:35

With Geth running, (I used geth --rinkeby --fast --cache=512 console --shh --rpc --rpccorsdomain '*' --ws --wsorigins '*'), and web3.js v1 in the browser, type into the browser console:

> web3.shh.setProvider("ws://localhost:8546") // must use websockets (read this on https://ethereum.stackexchange.com/a/34840/6014)
> web3.shh.newKeyPair().then(console.log) // logs key pair
> web3.shh.subscribe("messages", {privateKeyID: keyPair}, (err, msg) => console.log("msg received!", msg)) // returns subscription that will log the message when it's received
> web3.shh.getPublicKey(keyPair).then(console.log) // logs public key

> web3.shh.post({pubKey: publicKey, ttl: 10, payload: '0xffffffdddddd1122', powTime: 3, powTarget: 0.5}) // sends the message

This example works for Whisper v5: https://github.com/gballet/whisper-chat-example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.