Skip to content

Latest commit

 

History

History
254 lines (198 loc) · 11.2 KB

File metadata and controls

254 lines (198 loc) · 11.2 KB

shieldsIO shieldsIO shieldsIO

WideImg

Máster en Programación FullStack con JavaScript y Node.js

JS, Node.js, Frontend, Backend, Firebase, Express, Patrones, HTML5_APIs, Asincronía, Websockets, Testing

Clase 71

MQTT

esquema

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

Esquema con web

esquema

Recursos

Librerías de Real Time

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

Socket IO

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

Mosca

esquema

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

esquema

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

Node RED

logo

Funcionamieto

ejemplo

Recursos