0

I want to use the web3-shh functionality. I used the example from readthedocs(web3-shh). Everything works except for the subscribe operation. It raises following error:

UnhandledPromiseRejectionWarning: Error: Subscriptions are not supported with the HttpProvider.

Any idea of how to deal with this? The test program I used is

import Web3            from 'web3';
const uri = process.env.ETH_NODE_URI || 'http://localhost:8545';
var web3 = new Web3(new Web3.providers.HttpProvider(uri));

var identities = [];

Promise.all([
web3.shh.newSymKey().then((id) => {identities.push(id);}),
web3.shh.newKeyPair().then((id) => {identities.push(id);})
]).then(() => {
var subscription = web3.shh.subscribe('messages', {
    symKeyID: identities[0],
    topics: ['0xffaadd11']
}).on('data', console.log);
}).then(() => {
web3.shh.post({
    symKeyID: identities[0], // encrypts using the sym key ID                                                  
    sig: identities[1], // signs the message using the keyPair ID                                              
    topic: '0xffaadd11',
    payload: '0xffffffdddddd1122'
}).then(h => console.log(`Message with hash ${h} was successfuly sent`))
.catch(err => console.log("Error: ", err));
});

I would appreciate any advice on how to deal with this problem. Full examples in the documentation would be a good idea as well.

1
  • 1
    You have to use web socket provider for using subscriptions
    – Sanjay S B
    Commented Oct 3, 2019 at 8:20

1 Answer 1

0

As Sanjay noted above, you need websocket for whisper to perform its function of notifiying you of incoming messages in realtime.

Here's an example of how to create a Websocket provider (using ethjs, but web3.js is similar) https://github.com/invisible-college/democracy/blob/master/packages/whisper/src/whisper.js#L23

If you run your own whisper node, like geth or the status-go implementation (which I encourage you to do, as someone else's whisper node will be able to snoop your keypairs), you should start it with the --wsorigins="mychat" flag, where "mychat" could be anything as long as you choose the same value when creating the websocket provider in the client.

3
  • Thank you. That was helpful. Actually more needed to be added to geth command options: --ws --wsport=8546 Commented Oct 5, 2019 at 8:33
  • You're welcome, and thanks for the addition, I forgot to mention the other websocket flags for geth. A much faster and compact way to run a whisper node, btw, is the use the status-go docker image, documented here: gist.github.com/cryptogoth/4713e36c5cbde9d25a423700b22c3aab Sadly, the official documentation has been taken down, hopefully temporarily.
    – Paul Pham
    Commented Oct 7, 2019 at 7:32
  • Do you mind marking this question as answered? :)
    – Paul Pham
    Commented Oct 7, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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