nodejs et socketio en mode scalable dans le cloud - gab 2015

of 29 /29
#GlobalAzure #ViseoSpirit NodeJS & SocketIO scalables Stéphane Escandell @sescandell github/sescandell

Author: stephane-escandell

Post on 29-Jul-2015

289 views

Category:

Technology


0 download

Embed Size (px)

TRANSCRIPT

1. #GlobalAzure #ViseoSpirit 2. Microsoft Azure#GlobalAzure #ViseoSpirit GAB 2015 LYON ORAGANIZER LOCAL SPONSORS WORLDWIDE SPONSORS #GlobalAzure #ViseoSpirit 3. Microsoft Azure#GlobalAzure #ViseoSpirit Node Open source Cr en 2009 Bas sur le moteur V8 Application C Utilise en tant que gestionnaire de requtes HTTPs dans lexemple daujourdhui Event-driven IO non bloquants NodeJS 4. Microsoft Azure#GlobalAzure #ViseoSpirit Socket.IO JAVASCRIPT REALTIME WEBAPP BI-DIRECTIONNAL WEBSOCKETS UNIFIED API NODEJS FALLBACK EVENT DRIVEN ASYNCHRONOUS OPEN SOURCE BROADCASTING CLIENT SERVER WRAPPER DATA AJAX CROSS PLATFORM MULTI-CLIENT Bi-directionnal communication Emit message Broadcasting 5. Microsoft Azure#GlobalAzure #ViseoSpirit Application de chat Isolation par room Notifications Gravatar Fallback pulling Fil rouge 6. Microsoft Azure#GlobalAzure #ViseoSpirit 6 Fonctionnement de lapplication connect() connect load peopleInChat login img participantJoined msg receive Login process Notify users Chat loop Notify users 7. Microsoft Azure#GlobalAzure #ViseoSpirit EXCUTION LOCALE 8. Microsoft Azure#GlobalAzure #ViseoSpirit DevOps friendly Interface intuitive x-CLI Ouvert .Net PHP Java NodeJS Dploiement simplifi : git push azure CI : hooks Git Web App 9. Microsoft Azure#GlobalAzure #ViseoSpirit Windows Server Serveur web IIS Scalabilit automatique Pas de root Pas daccs RDP aux machines Architecture Web App 9 10. Microsoft Azure#GlobalAzure #ViseoSpirit Pas de changement de lapplication Prparation de lenvironnement Importation dun profil de scurisation Excut une seule fois Selon votre prfrence : Portail en ligne x-CLI API Rest Dploiement 10 11. Microsoft Azure#GlobalAzure #ViseoSpirit azure site create gab2015demo --location "West Europe --git --gitusername "sescandell" git push azure local:master 11 12. Microsoft Azure#GlobalAzure #ViseoSpirit Activation des websockets azure site set w gab2015demo 12 13. Microsoft Azure#GlobalAzure #ViseoSpirit EXCUTION DEPUIS WEB APP 14. Microsoft Azure#GlobalAzure #ViseoSpirit Quels problmes pour la scalabilit ? 1 1 2 2 2 Les messages ne sont pas envoys (broadcasts) aux diffrentes instances socket.io Le stockage des participant est local (non partag) 1 2 15. Microsoft Azure#GlobalAzure #ViseoSpirit Architecture cible 15 Stockage des participants bas sur Azure Table Storage (un stockage cl-valeur) Azure Service Bus service utilis comme connecteur inter-scokets (compatible protocole AMQP) Point dentre de lapplication Web App Agit comme un Load Balancer Une instance 16. Microsoft Azure#GlobalAzure #ViseoSpirit 1re tape : Adapter SocketIO - Service Bus require(azure); module.exports = adapter; function adapter(opts) { ... var azureServiceBusSender = azure.createServiceBusService(); var azureServiceBusReceiver = azure.createServiceBusService(); function AzureServiceBus(namespace) { azureServiceBusReceiver.createTopicIfNotExists("default", function(err) { azureServiceBusReceiver.createSubscription("default",getId(), function(err) { azureServiceBusReceiver.receiveSubscriptionMessage("default",getId(),this.onmessage.bind(this)); } }) } AzureServiceBus.prototype.__proto__ = Adapter.prototype; AzureServiceBus.prototype.onmessage = function(err, receivedMessage) { azureServiceBusReceiver.receiveSubscriptionMessage(opts.topic, this.subscriptionId, this.onmessage.bind(this)); args.push(true); this.broadcast.apply(this, args); }; 16 17. Microsoft Azure#GlobalAzure #ViseoSpirit AzureServiceBus.prototype.broadcast = function(packet, opt, remote) { Adapter.prototype.broadcast.call(this, packet, opt); if (!remote) { var message = { body: msgpack.encode([packet, opt]) }; azureServiceBusSender.sendTopicMessage("default", message); } }; return AzureServiceBus; } 17 18. Microsoft Azure#GlobalAzure #ViseoSpirit EXCUTION DEPUIS WEB APP AVEC SOCKET.IO 19. Microsoft Azure#GlobalAzure #ViseoSpirit Service Bus & NodeJS Place Redis PubSub 20. Microsoft Azure#GlobalAzure #ViseoSpirit Nouvelle architecture cible 20 Stockage des participants bas sur Azure Table Storage (un stockage cl- valeur) Azure Redis Cache (via ses capacits pub/sub) service utilis comme connecteur pour socket.IO Point dentre de lapplication Web App Agit comme un Load Balancer Une instance 21. Microsoft Azure#GlobalAzure #ViseoSpirit 1re tape : Adapter SocketIO - Redis var redis = require(redis).createClient; var redisAdapter = require('socket.io-redis'); module.exports = function (app, io) { ... // Publisher var redisClientPub = redis( process.env.REDIS_PORT, process.env.REDIS_HOST, {auth_pass: process.env.REDIS_AUTH_PASS} ); // Subscriber var redisClientSub = redis( process.env.REDIS_PORT, process.env.REDIS_HOST, {auth_pass: process.env.REDIS_AUTH_PASS, detect_buffers: true} ); io.adapter(redisAdapter({ pubClient: redisClientPub, subClient: redisClientSub })); 21 22. Microsoft Azure#GlobalAzure #ViseoSpirit 2me tape : Stockage Azure Table Storage var azureStorage = require('azure-storage'); module.exports = function () { // Service Repository var ParticipantTableStorage = function () { var azureTableService = azureStorage.createTableService(); var entGen = azureStorage.TableUtilities.entityGenerator; var tableName = 'participants'; var partitionKey = "default"; var prefix = process.env.WEBSITE_INSTANCE_ID || 'default'; // If this is the first time the application is launched, let's create the table azureTableService.createTableIfNotExists(tableName); return { 22 23. Microsoft Azure#GlobalAzure #ViseoSpirit // Retrieve participants count from Store // We retrieve all participants to show you how to get a users' list getCount: function(room, callback) { var query = new azureStorage.TableQuery() .where('PartitionKey eq ?', partitionKey + room); azureTableService.queryEntities(tableName, query, null, function(err, result, response){ callback(undefined, result.entries.length); }.bind(this)) }, // Add a new participant to the store add: function(participant) { var entity = { PartitionKey: entGen.String(partitionKey + participant.room), RowKey: entGen.String(prefix + participant.id), username: entGen.String(participant.username), avatar: entGen.String(participant.avatar), room: entGen.String(participant.room) }; azureTableService.insertEntity(tableName, entity) }, 23 24. Microsoft Azure#GlobalAzure #ViseoSpirit // Remove a participant from the store remove: function(participant) { var entity = { PartitionKey: entGen.String(partitionKey + participant.room), RowKey: entGen.String(prefix + participant.id) }; azureTableService.deleteEntity(tableName, entity); } }; return ParticipantTableStorage; }; 24 25. Microsoft Azure#GlobalAzure #ViseoSpirit WEB APP / LOCAL / SOCKET IO / REDIS 26. Microsoft Azure#GlobalAzure #ViseoSpirit Pour faire de la potion magique, mieux vaut avoir la bonne recette 26 27. Microsoft Azure#GlobalAzure #ViseoSpirit Devrait tre une rgle de base Crez des interfaces communes Rgles dquipes TypeScript Injection de dpendances Stockage de donnes Systme de fichiers Gestionnaire dvnement toutes les I/Os I/O abstraction 27 28. Microsoft Azure#GlobalAzure #ViseoSpirit GAB 2015 LYON ORAGANIZER LOCAL SPONSORS WORLDWIDE SPONSORS #GlobalAzure #ViseoSpirit 29. Microsoft Azure#GlobalAzure #ViseoSpirit je peux essayer dy rpondre Vous avez des questions