node package manager
It’s your turn. Help us improve JavaScript. Take the 2017 JavaScript Ecosystem Survey »



An authentication/authorization module for mosca using Auth0.

See this article for details.

Setting up mosca server

var mosca = require('mosca')
var Auth0Mosca = require('./auth0Mosca');
var settings = {
  port: 9999,
var auth0 = new Auth0Mosca('', '{AUTH0 CLIENT ID}', '{AUTH0 CLIENT SECRET}','Thermostats');
//Setup the Mosca server
var server = new mosca.Server(settings);
//Wire up Authentication & Authorization
server.authenticate = auth0.authenticateWithCredentials(); //Using usr/pwd
server.authorizePublish = auth0.authorizePublish();
server.authorizeSubscribe = auth0.authorizeSubscribe();
server.on('ready', setup);
// Fired when the mqtt server is ready
function setup() {
   console.log('Mosca server is up and running');
server.on('clientConnected', function(client) {
  console.log('New connection: ', );

The client:

var mqtt = require('mqtt')
  , host = 'localhost'
  , port = '9999';
var settings = {
  keepalive: 1000,
  protocolId: 'MQIsdp',
  protocolVersion: 3,
  clientId: 'Thermostat 1a',
  username:'the device username',
  password: 'the device password'
// client connection
var mqttClient = mqtt.createClient(port, host, settings);
setInterval(sendTemperature, 2000, mqttClient);
function sendTemperature(client){
  console.log("Sending event");
    var t = {
        T: Math.random() * 100,
        Units: "C"
    client.publish('temperature', JSON.stringify(t));

Notice that the auth0Mosca module also supports authetnicating with Json Web Tokens directly. Devices will obtain JWT independently and send it on the password field. In this case username must be JWT by convention.