title: Golang-Paho MQTT Subscriber keeps disconnecting with error EOF after reinitialization of subscriber
body: <p>I'm trying to change mqtt client handlers and certificates dynamically, which causing subscriber EOF, when subscriber and publisher is connected</p>
  <p>This is what I'm trying to do,</p>
  <p>1] I'm initializing subscriber/publisher (<strong>using firstPubHandler, firstConnectHandler and default certificates</strong>)</p>
  <p>2] Sending registration message on server using publisher to get new certificates details</p>
  <p>3] Server will response back with certificate details, that response will be handled by firstConnectHandler on topic <em><strong>.../id/Certificate</strong></em> to download certificates.</p>
  <p>4] firstPubHandler will handle response by server and reinitialize publisher/subscriber (<strong>using messagePubHandler, connectHandler and newly downloaded certificates</strong>), connectHandler will listen for all topics <em><strong>/id/+</strong></em></p>
  <p>Everything works good, except <em><strong>when I reinitialize subscriber/publisher, subscriber keeps disconnecting with error &quot;EOF&quot;</strong></em></p>
  <p>Am I doing anything wrong here? or is there any better way to accomplish this?
  Any help is appreciated</p>
  <p>-- Main function</p>
  <pre><code>var opt Params
  var publisher mqtt.Client
  var subscriber mqtt.Client
  
  func main() {
      InitializeBroker(firstPubHandler, firstConnectHandler)
  
      //Ultimately it will trigger message on &quot;.../id/Certificate&quot; topic which will be handled byfirstConnectHandler 
      PublishRegistrationMessage(publisher)
  
      c := make(chan os.Signal, 1)
      signal.Notify(c, os.Interrupt, syscall.SIGTERM)
      done := make(chan os.Signal, 1)
      signal.Notify(done, os.Interrupt, syscall.SIGTERM)
      go func() {
          for {
          }
      }()
      &lt;-done
      &lt;-c
      DisconnectBrocker()
  }
  </code></pre>
  <p>-- Handlers</p>
  <pre><code>// First handlers
  var firstPubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
      DownloadCertificates(msg.Payload())
      InitializeBroker(messagePubHandler, connectHandler)
  }
  
  var firstConnectHandler mqtt.OnConnectHandler = func(c mqtt.Client) {
      if token := c.Subscribe(opt.SubClientId+&quot;/id/Certificate&quot;, 0, firstPubHandler); token.Wait() &amp;&amp; token.Error() != nil {
          log.Error(token.Error())
      }
  }
  
  // Second handlers
  var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
      ProcessMessage(msg.Payload())
  }
  
  var connectHandler mqtt.OnConnectHandler = func(c mqtt.Client) {
      if token := c.Subscribe(opt.SubClientId+&quot;/id/+&quot;, 0, messagePubHandler); token.Wait() &amp;&amp; token.Error() != nil {
          log.Error(token.Error())
      }
  }
  
  // Common handler
  var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
      log.Info(err)
  }
  </code></pre>
  <p>-- Mqtt broker initialization</p>
  <pre><code>func InitializeBroker(lMessageHandler mqtt.MessageHandler, lConnectHandler mqtt.OnConnectHandler) {
      statusPublishTopic := opt.PubClientId/id
      nodeSubscribeTopic := opt.SubClientId/id
  
      // Build the options for the publish client
      publisherOptions := mqtt.NewClientOptions()
      publisherOptions.AddBroker(opt.Broker)
      publisherOptions.SetClientID(statusPublishTopic)
      publisherOptions.SetDefaultPublishHandler(lMessageHandler)
      publisherOptions.OnConnectionLost = connectLostHandler
  
      // Build the options for the subscribe client
      subscriberOptions := mqtt.NewClientOptions()
      subscriberOptions.AddBroker(opt.Broker)
      subscriberOptions.SetClientID(nodeSubscribeTopic)
      subscriberOptions.SetDefaultPublishHandler(lMessageHandler)
      subscriberOptions.OnConnectionLost = connectLostHandler
      subscriberOptions.OnConnect = lConnectHandler
  
      if !opt.NoTLS {
          tlsconfig, err := NewTLSConfig()
          if err != nil {
              log.Fatalf(err)
          }
  
          subscriberOptions.SetTLSConfig(tlsconfig)
          publisherOptions.SetTLSConfig(tlsconfig)
      }
  
      publisher = mqtt.NewClient(publisherOptions)
      if token := publisher.Connect(); token.Wait() &amp;&amp; token.Error() != nil {
          log.Fatalf(token.Error())
      }
  
      subscriber = mqtt.NewClient(subscriberOptions)
      if token := subscriber.Connect(); token.Wait() &amp;&amp; token.Error() != nil {
          log.Fatalf(token.Error())
      }
  }
  
  func NewTLSConfig() (config *tls.Config, err error) {
      certpool := x509.NewCertPool()
      pemCerts, err := ioutil.ReadFile(rootCert)
      if err != nil {
          return nil, err
      }
      certpool.AppendCertsFromPEM(pemCerts)
  
      cert, err := tls.LoadX509KeyPair(nodeCertFilePath, pvtKeyFilePath)
      if err != nil {
          return nil, err
      }
  
      config = &amp;tls.Config{
          RootCAs:      certpool,
          ClientAuth:   tls.NoClientCert,
          ClientCAs:    nil,
          Certificates: []tls.Certificate{cert},
      }
      return config, nil
  }
  </code></pre>