John Abraham
John Abraham

Reputation: 18781

how to connect to an mqtt broker on ec2 via mqtt.js?

I want: to push a message from a raspberrypi via node package mqtt.js to/through a ec2 mosquitto broker and back to the raspberrypi.

I've installed a mosquitto broker on my ec2 instance. using these commands:

ssh -i awskeypair.pem [email protected]
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update 
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
mosquitto

and on a raspberrypi I've installed mqtt.js via node.

installed package:

npm install mqtt --save

index.js

var mqtt    = require('mqtt');
// pretty sure this var client line isn't connecting if I use test.mosquitto.org it works just fine
var client  = mqtt.connect('mqtt://ec2-54-153-18-31.us-west-1.compute.amazonaws.com');

client.subscribe('presence');
client.publish('presence', 'Hello mqtt');

client.on('message', function (topic, message) {
  // message is Buffer 
  console.log(message.toString());
});

client.end();

Problem: I'm pretty sure my ec2 instance with the mosquitto broker isn't accessible


QUESTION:

How do I connect to my ec2 broker?


notes on my broker

ubuntu@ip-172-31-6-23:~$ mosquitto
1425504937: mosquitto version 1.4 (build date 2015-02-18 21:33:29+0000) starting
1425504937: Using default config.
1425504937: Opening ipv4 listen socket on port 1883.

flow diagram

Upvotes: 1

Views: 2065

Answers (2)

Spinningbull
Spinningbull

Reputation: 21

Not sure if this relates to ec2 as well, but for IoT I got MQTT.js working by setting up the options object correctly in the connect call.

var client = mqtt.connect('mqtts://<yourawsid>.iot.us-east-1.amazonaws.com', {
    port: '8883',
    cert: fs.readFileSync('<path>/cert.pem'),
    key: fs.readFileSync('<path>/privateKey.pem')
    }
);

Upvotes: 2

Dominik Obermaier
Dominik Obermaier

Reputation: 5770

Sounds like you didn't open the port 1883 in your security group. If the security group is not the problem, double check that you don't have IPTables running on your EC2 instance

Upvotes: 3

Related Questions