I've been digging for hours for an easy solution to this question and I can't find one. How can I get started with Socket.io after using Express's application generator. For example, if I create a new app with
express myapp
and npm install --save socket.io
how do I use in in my index.jade file?
My index.jade file is plain HTML right now and looks as such
html.
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('message', function(msg){
console.log('you got a message');
});
</script>
</head>
<body>
<canvas id="mycanvas" width="768" height="720"></canvas>
</body>
My routes/index.js
file looks as such
var express = require('express');
var router = express.Router();
var io = require('socket.io')();
router.get('/', function(req, res, next) {
io.emit('text', { for: 'everyone' });
res.render('index', { title: 'Express' });
});
module.exports = router;
Nothing happens when I go to my app's homepage though. I followed this SO answer but my app only fires the "A user connected" message. My io.emit
never fires in my index.js file.