install socket.io
npm i socket.io
socket io
const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
client.on('event', data => { /* … */ });
client.on('disconnect', () => { /* … */ });
});
server.listen(3000);
socket io
It is a library that enables real-time, bidirectional and event-based communication between the browser and the server.
socket io
npm install socket.io
socket io
//Importamos la app
const app = require('./app');
const socketIO = require('socket.io');
//Con el modulo 'dotenv' importamos las variables de entorno
//contenidas dentro del archivo '.env'
//Se recomienda correr este proyecto con permisos de administrador
require('dotenv').config();
//Importamos la conexion a la base de datos dentro de 'database.js'
require('./database/database');
//Esta funcion ejecuta el servidor
async function main(){
//Obtenemos el puerto
const PORT = app.get('port');
//Escuchamos el servidor en puerto y con '0.0.0.0' podemos acceder publicamente
server = await app.listen(PORT, '0.0.0.0');
const io=socketIO.listen(server);
//console.log('io index: ', io)
/*
io.on('connection', function(){
console.log('Socket Conectado');
io.emit('Prueba', 'Hola soy el servidor'.toString());
});
*/
//Mostramos por consola
console.log('Servidor ejecutandose en el puerto: ', PORT);
> };
main();
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us