title: Mosquitto security error: OpenSSL Error: routines:SSL3_GET_RECORD:wrong version number
body: <p>I'm trying to store MQTT payloads in a MongoDB database using NodeJS.
  When I run my code I get the following error popped up at the Mosquitto server: </p>
  
  <pre><code>1427756032: Socket error on client &lt;unknown&gt;, disconnecting.
  1427756033: New connection from 146.175.138.141 on port 8883.
  1427756033: OpenSSL Error: error:1408F10B:SSL routines:SSL3_GET_RECORD:wrong version number
  </code></pre>
  
  <p>I'm not the only one facing this error but the proposed solutions on google won't work for me. </p>
  
  <p>I'm working in a Ubuntu14.04 TLS (trusty) server environment. The version of OpenSSL which I used to make my own keys and certificates is:     </p>
  
  <pre><code>OpenSSL 1.0.1f 6 Jan 2014
  </code></pre>
  
  <p>For making these keys I followed the <a href="http://mosquitto.org/man/mosquitto-tls-7.html" rel="nofollow">manual</a> of Mosquitto.</p>
  
  <p>The version of nodejs is:</p>
  
  <pre><code>v0.10.25
  </code></pre>
  
  <p>The configuration file of mosquitto:</p>
  
  <pre><code>port 8883
  cafile /etc/keys/ca.crt
  certfile /etc/keys/server.crt
  keyfile /etc/keys/server.key
  tls_version tlsv1
  require_certificate true
  </code></pre>
  
  <p>The nodejs file:</p>
  
  <pre><code>var mqtt=require('mqtt')
  var mongodbClient=require('mongodb').MongoClient;
  var deviceRoot="demo/device/"
  var mqtthost = '146.175.138.141';
  var KEY = '/etc/keys/client.key';
  var CERT = '/etc/keys/client.crt';
  var CAfile = '/etc/keys/ca.crt';
  
  var options = {
      host: mqtthost,
      port: 8883,
      protocolId: 'MQIsdp',
      ca: CAfile,
      keyPath: KEY,
      certPath: CERT,
      secureProtocol: 'TLSv1_method',
      protocolId: 'MQIsdp',
      protocolVersion: 3
  };
  
  var collection,client;
  
  mongodbClient.connect("mongodb://localhost:27017/exampleDb", function(err,db){
   if(err){return console.dir(err);}
  
   collection=db.collection("test_mqtt");
  
   client=mqtt.connect(options);
  
   client.subscribe("#");
   client.publish(deviceRoot, '21');
  
   client.on('message', function(topic,payload){
    str = payload.toString();
    console.log(str);
    var key=topic.replace(deviceRoot,'');
  
    collection.update(
     { _id:key },
     { $push: { events: { event: { value:str, when:new Date() } } } },
     { upsert:true }
  )})})
  </code></pre>
  
  <p>The keys should be working because publishing with the following command is not a problem:</p>
  
  <pre><code>mosquitto_pub -h 146.175.138.141 -p 8883 -t Server -m helloworld --cafile /etc/keys/ca.crt --cert /etc/keys/client.crt --key /etc/keys/client.key --tls-version tlsv1
  </code></pre>
  
  <p>Any idea what I'm doing wrong?</p>