0

I'm trying to add instant messaging to an existing app. But I'm not sure how should configure socket.io module. I've already tried the following:

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = require('./config/config.js');

var expressConfig = require('./config/express.js').front,
  models = require('./app/models'),
  passConfig = require('./config/passport-front.js');

const app = expressConfig();
const passport = passConfig();

app.set('port', config.frontPort);
const http = require('http').Server(app);
const io = require('socket.io')(http);

io.on('connection', function (socket) {
  console.log('Connected');
});

models.sequelize.sync().then(function () {
  var server = http.listen(app.get('port'), function () {
    console.log('Express server listening on port ' + server.address().port);
  });

});

module.exports = http;

Since connected is not being logged when I open a page I'm assuming something is missing. Where is my mistake?

belyid
  • 841
  • 2
  • 12
  • 28

1 Answers1

0

You didn't show your client-side code so it's hard to tell you what's wrong. Do you get any error in the browser? What is the network traffic on the browser? Without that it's hard to tell anything specific.

Take a look at this answer:

It has a working example of something similar to what you're trying to do.

In general - remember that you need to include the socket.io client-side code in the browser:

<script src="/socket.io/socket.io.js"></script>

and then you need to connect with io() with something like:

var s = io();
s.on('color', function (color) {
  document.body.style.backgroundColor = color;
});

The 'color' here is just an example that comes from this project on GitHub:

It's an example of a website that changes colors selected in messages that come vie socket.io.

rsp
  • 107,747
  • 29
  • 201
  • 177
  • Thank you. It appears I was waiting for magic since I never wrote any code on client side. I missed the simple `io.connect()`. – belyid May 30 '17 at 16:44