description: Tutorial on whisper with Go.

Connecting Whisper Client

To use whisper, we must first connect to an Ethereum node running whisper. Unfortunately, public gateways such as infura don’t support whisper because there is no incentive for processing the messages for free. Infura might support whisper in the near future but for now we must run our own geth node. Once you install geth, run it with the --shh flag on to enable the whisper protocol, as well as the --ws flag to enable websocket support in order to receive messages in real time, and also enable the --rpc flag because we’ll be communicating over RPC.

  1. geth --rpc --shh --ws

Now in our Go application we’ll import the go-ethereum whisper client package found at whisper/shhclient and initialize the client to connect our local geth node over websockets using the default websocket port 8546.

  1. client, err := shhclient.Dial("ws://127.0.0.1:8546")
  2. if err != nil {
  3. log.Fatal(err)
  4. }
  5. _ = client // we'll be using this in the next section

Now that we’re dialed in let’s create a key pair for encrypting the message before we send it in the next section.

Full code

Commands

  1. geth --rpc --shh --ws

whisper_client.go

  1. package main
  2. import (
  3. "log"
  4. "github.com/ethereum/go-ethereum/whisper/shhclient"
  5. )
  6. func main() {
  7. client, err := shhclient.Dial("ws://127.0.0.1:8546")
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. _ = client // we'll be using this in the next section
  12. fmt.Println("we have a whisper connection")
  13. }