Chat socket io

app.js

 

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendfile('index.html');
});
users = [];
io.on('connection', function(socket){
  console.log('A user connected');
  socket.on('setUsername', function(data){
    console.log(data);
    if(users.indexOf(data) > -1){
      socket.emit('userExists', data + ' username is taken! Try some other username.');
    }
    else{
      users.push(data);
      socket.emit('userSet', {username: data});
    }
  });
  socket.on('msg', function(data){
      //Send message to everyone
      io.sockets.emit('newmsg', data);
  })
});
http.listen(3000, function(){
  console.log('listening on localhost:3000');
});

index.html

 <!DOCTYPE html>
<html>
 <head><title>Hello world</title></head>
 <script src="/socket.io/socket.io.js"></script>
 <script>
 var socket = io();
 function setUsername(){
 socket.emit('setUsername', document.getElementById('name').value);
 };
 var user;
 socket.on('userExists', function(data){
 document.getElementById('error-container').innerHTML = data;
 });
 socket.on('userSet', function(data){
 user = data.username;
 document.body.innerHTML = '<input type="text" id="message">\
 <button type="button" name="button" onclick="sendMessage()">Send</button>\
 <div id="message-container"></div>';
 });
 function sendMessage(){
 var msg = document.getElementById('message').value;
 if(msg){
 socket.emit('msg', {message: msg, user: user});
 }
 }
 socket.on('newmsg', function(data){
 if(user){
 document.getElementById('message-container').innerHTML += '<div><b>' + data.user + '</b>: ' + data.message + '</div>'
 }
 })
 </script>
 <body>
 <div id="error-container"></div>
 <input id="name" type="text" name="name" value="" placeholder="Enter your name!">
 <button type="button" name="button" onclick="setUsername()">Let me chat!</button>
 </body>
</html>

Ejemplo promesa

function promiseSqrt(value){
return new Promise(function (fulfill, reject){
console.log(‘START execution with value =’, value);
setTimeout(function() {
fulfill({ value: value, result: value * value });
}, 0 | Math.random() * 1000);
});
}

var p = [0,1,2,3,4,5,6,7,8,9];
p.reduce(
function (sequence, value) {
return sequence.then(function() {
return promiseSqrt(value);
}).then(function(obj) {
console.log(‘END execution with value =’, obj.value,
‘and result =’, obj.result);
});
},
Promise.resolve()
).then(function() {
console.log(‘COMPLETED’);
});

Ejemplo de multihilo con node

function longRunningOperation(callback) {
// simulate a 3 second operation
setTimeout(callback, 3000);
}

function userClicked() {
console.log(‘starting a long operation’);
longRunningOperation(function () {
console.log(‘ending a long operation’);
});
}
// simulate a user action
userClicked();

 

Cada uno con su closure:

function longRunningOperation(callback) {
// simulate a 3 second operation
setTimeout(callback, 3000);
}

function webRequest(request) {
console.log(‘starting a long operation for request:’, request.id);
longRunningOperation(function () {
console.log(‘ending a long operation for request:’, request.id);
});
}
// simulate a web request
webRequest({ id: 1 });
// simulate a second web request
webRequest({ id: 2 });

IDEs para node

https://www.jetbrains.com/webstorm/

https://netbeans.org/

https://www.visualstudio.com/

https://www.slant.co/topics/46/~best-ides-for-node-js