Socket.IO és una llibreria JavaScript per a la creació d'aplicacions web en temps real (realtime web applications). Amb socket.io podem tenir comunicacions en temps real i bidireccional entre clients i servidors web. Socket.io té dos parts la part de client (client-side library) i la server-side que s'executa en node.js. Els dos components tenen una API idèntica i com node.js són event-driven.
Socket.IO primariament utilitza el protocol WebSocket amb polling com una opció de fallback quan el protocol no està disponible tot això sense canviar l'API/Interficie de l'aplicació (és a dir sense canviar el codi). Està és la raó per la que sovint es confonen WebSockets i socket.io que tot i que estan molt relacionats no són exactament el mateix i cal tenir en compte a més que socket.io ofereix moltes més funcionalitats com broadcasting, Redis), emmagatzemar dades associades a cada client, asynchronous I/O, etc.
Llegiu abans article websockets per entendre el concepte dels websockets o HTTP sockets
Per poder utilitzar socket.io a la banda de servidor cal instal·lar Node.js que inclou la eina npm
Només cal un script javascript a la web o millor encara si utilitzeu algun sistema com Browserify o webpack
Cal instal·lar node.js i executar:
$ npm install socket.io --save
Es pot utilitzar npm i els fitxers package.json per definir socket.io com un dependència i poder instal·lar amb :
$ npm install
NOTA: Similar a molts altres sistemes de paquets/dependències com p.ex. Composer o Bower
Ara ja podeu escriure codi que utilitzi socket.io. Vegeu els exemples i el Hello world de més avall
Al client podem instal·lar socket io com qualsevol altre llibreria Javascript, podeu trobar les instruccions a:
http://socket.io/download/
Es pot instal·lar en local el fitxer socket.io.js però millor utilitzar un CDN:
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
És important que tingueu en compte que a on instal·leu/executeu un servidor socket.io (vegeu apartat anterior) també es posa a disposició dels clients el script:
<script src="http://your-io-server/socket.io/socket.io.js">
IMPORTANT: podeu fer una ullada Browserify o Webpack per a la gestió de les dependències i l'ús en navegador (client) de les funcionalitats server side de node.js
Instal·leu:
$ npm install socket.io --save
var io = require('socket.io')(); io.on('connection', function(client){}); io.listen(3000);
Resources:
Instal·leu:
$ npm install socket.io --save
El servidor htp no s'ha d'instal·lar està de sèrie amb node.js:
var server = require('http').createServer(); var io = require('socket.io')(server); io.on('connection', function(client){ client.on('event', function(data){}); client.on('disconnect', function(){}); }); server.listen(3000);
Resources:
var app = require('express')(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(){ /* … */ }); server.listen(3000);
Vegeu també:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io('http://localhost'); socket.on('connect', function(){}); socket.on('event', function(data){}); socket.on('disconnect', function(){}); </script>
Socket.IO és compatible amb browserify i altres Bundle packagers com webpacks. Vegeu també vue-cli.
Instal·lació:
$ npm install socket.io-client --save
Ús:
var socket = require('socket.io-client')('http://localhost'); socket.on('connect', function(){}); socket.on('event', function(data){}); socket.on('disconnect', function(){});
http://danielnill.com/nodejs-tutorial-with-socketio/ Codi font: https://github.com/DanielNIll/node-and-socket-example
http://socket.io/blog/native-socket-io-and-android/
From https://github.com/LearnBoost/engine.io. TODO:
The main goal of Engine.io is ensuring the most reliable realtime communication. Unlike the previous socket.io core, it always establishes a long-polling connection first, then tries to upgrade to better transports that are "tested" on the side. During the lifetime of the socket.io projects, we've found countless drawbacks to relying on HTML5 WebSocket or Flash Socket as the first connection mechanisms.Both are clearly the right way of establishing a bidirectional communication, with HTML5 WebSocket being the way of the future. However, to answer most business needs, alternative traditional HTTP 1.1 mechanisms are just as good as delivering the same solution.
Engine is to Socket.IO what Connect is to Express.
If you want the lower level abstraction, use engine.io. If you want a websocket abstraction, keep using socket.io. Engine.io is of more interest to you if you're building a library/framework on top of socket.io.
socket.io is of more interest to you if you're building an application on top of socket.io. socket.io is built on top of engine.io.
socket.io is engine.io with bells and whistles.If you don't need everything socket.io has (redis store, groups, etc.) just use engine.
Vegeu Laravel Broadcasting Events