JS, Node.js, Frontend, Backend, Firebase, Express, Patrones, HTML5_APIs, Asincronía, Websockets, Testing
MQTT es un protocolo de mensajería basado en ISO estándar publicación-suscripción. Funciona sobre el protocolo TCP / IP. Está diseñado para conexiones con ubicaciones remotas donde se requiere una "huella de código pequeño" o el ancho de banda de la red es limitado Wikipedia
Funcionamiento del protocolo
Esquema con web
Recursos
- How to Build an High Availability - MQTT Cluster for the Internet of Things
- IBM | Conociendo MQTT
- MQTT: un protocolo específico para el internet de las cosas
MQTT
- MQTT.js The MQTT client for Node.js and the browser
- Mosca MQTT broker as a module
- aedes Barebone MQTT broker that can run on any stream server, the node way
- ascoltatori The pub/sub library for node backed by Redis, MongoDB, AMQP (RabbitMQ), ZeroMQ, MQTT (Mosquitto) or just plain node!
Web Sockets
- socket.io Realtime application framework (Node.JS server)
- sockjs WebSocket emulation - Node.js server
- uWebSockets Tiny WebSockets
- SocketCluster Highly scalable realtime framework
- engine.io Engine.IO is the implementation of transport-based cross-browser/cross-device bi-directional communication layer for Socket.IO.
- Kalmjs The MQTT client for Node.js and the browser
- rpc-websockets JSON-RPC 2.0 implementation over WebSockets for Node.js and JavaScript
- deepstream.io-client-js The Browser / Node.js Client for deepstream.io
Otras
- Faye Simple pub/sub messaging for the web
- Primus ⚡️ Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.
- node-browserchannel An implementation of a google browserchannel server in node.js
Servidor con http
public/index.html
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var socket = io("http://localhost:3000");
// use your socket
socket.on("welcome", (message) => {
// do something with the message.
})
</script>
server.js
const app = require('http').createServer(handler);
const io = require('socket.io')(app);
const fs = require('fs');
app.listen(80);
function handler (req, res) {
fs.readFile(`${__dirname}/index.html`,
(err, data) => {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', socket => {
socket.emit('news', { hello: 'world' });
socket.on('my other event', data => {
console.log(data);
});
});
Servidor con Express
public/index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
server.js
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
io.on('connection', socket => {
console.log('a user connected');
});
http.listen(3000, () => {
console.log('listening on *:3000');
});
Recursos
- Web Site
- Socket.io | Blog
- Socket.io Docs | Overview
- Socket.io | Demos
- Socket.io Docs | Server
- Socket.io Docs | Client
- Socket.io Docs | Introduction
- Socket.io Docs | The web framework
- Socket.io Docs | Serving HTML
- Socket.io Docs | Integrating Socket.IO
- Socket.io Docs | Emitting events
- Socket.io Docs | Broadcasting
- Socket.io Docs | Homework
- Socket.io Docs | Getting this example
- How to build a real time chat application in Node.js using Express, Mongoose and Socket.io
- Carlos Azaustre | WebSockets: Cómo utilizar Socket.io en tus aplicaciones web
- Introducción a Socket.io #nodejs
- Servidor real time con socket.io
- You don't need express to get started with socket.io
Claves
- Standalone con
$ mosca
- Puede embeberse en otras aplicaciones
- Autenticable con APIs
- Soporta AMQP, Mongo, Redis, y MQTT como pub/sub backends
- Necesita una base de datos como LevelDB, Mongo, o Redis
- Soporta websockets
- Rápido, 10k+ mensajes ruteados por segundo
- Escalable, 10k+ conexiones concurrentes
Esquema de funcionamiento
Servidor standalone
var mosca = require('mosca');
var ascoltatore = {
//using ascoltatore
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
var settings = {
port: 1883,
backend: ascoltatore
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.payload);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running');
}
Recursos
- Mosca Wiki
- Mosca Wiki | MQTT over Websockets
- Mosca Wiki | Authentication & Authorization
- Mosca Wiki | Docker support
- Mosca Wiki | FAQ (Frequently asked questions)
- Mosca Wiki | Mosca & Redis & Ascoltatori
- Mosca Wiki | Mosca advanced usage
- Mosca Wiki | Mosca as a standalone service
- Mosca Wiki | Mosca basic usage
- Mosca Wiki | Mosca Showcase
- Mosca Wiki | Persistence support
- Mosca Wiki | TLS SSL Configuration
- Mosca Wiki | Under the hood aka how mosca works
- Mosca in Github
- MQTT and Node.js - Messaging in the Internet of Things
- Setting up private MQTT broker using Mosca in Node.js
- Control your home using only Javascript
- Real time communications between IoT devices and Front-End JS apps
Recursos
- Node-RED
- Node-RED | Blog
- Node-RED | Docs
- Node-RED | Flows
- Node-RED Docs | Getting Started
- Node-RED Docs | User Guide
- Node-RED Docs | Node-RED Cookbook
- Node-RED Docs | Creating Nodes
- Node-RED Docs | Developing Node-RED
- Node-RED Docs | API Reference
- Github
- Introducción a Node-RED y Raspberry Pi con un sistema de alarma con Arduino
- Node Red in 5 minutes
- Intro to Node-RED: Part 1 Fundamentals
- Node-RED un software para dominarlos a todos
- Programación Visual con Node-Red: Conectando el Internet de las Cosas con Facilidad