How to use socket.io within a control that is injected by the consign?

Asked

Viewed 91 times

1

I have my file server.js:

const express = require( 'express' );
const expressValidator = require( 'express-validator' );
const expressSession = require( 'express-session' );
const consign = require( 'consign' );
const bodyParser = require( 'body-parser' );

const app = express();

app.use( bodyParser.urlencoded( { extended: true } ) );
app.use( bodyParser.json( { extended: true } ) );
app.use( expressValidator() );

consign()
   .include( 'app/controller' )
   .then( 'app/model' )
   .then( 'app/routes' )
   .then( 'config/dbConnection.js' )
   .into( app );

module.exports = app;

And the file app.js:

const app = require('./config/server');

const server = app.listen(80, () => console.log('Servidor online'));

const io = require('socket.io').listen(server);

io.on('connection', (socket) => {
console.log('Cliente conectado.');
socket.emit('news', { hello: 'bem vindo cabra!' });

socket.on('cliente', (data) => {
  console.log(data.my);
});
});

How do I use the variable io within the controls that are injected into app for consign?

  • Why don’t you inject the io in Consign also?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.