Reputation: 45
I'm trying to teach myself socket.io and node.js by following this tutorial: https://www.youtube.com/watch?v=tzNOqcoupWQ
This is the code that's written by the end of the turotial:
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const path = require('path');
const port = process.env.Port || 3000;
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
})
server.listen(port, function(){
console.log('Listening on port ' + port);
io.on('connection', function(socket){
console.log('User Connected! :)');
//handle a new message
socket.on('new:message', function(msgObj){
io.emit('new:message', msgObj);
});
//handle a new member joining
socket.on('new:member', function(name){
io.emit('new:member', name);
});
})
});
The problem is, it seems the io.on('connection' method isn't going through and the "User connected!" message isn't being printed out to the console, yet my index.html loads up just fine (it's just a simple header for now).
What can I do to solve this?
[EDIT] Here's the index.html
<h1>Hello, World!</h1>
Upvotes: 1
Views: 53
Reputation: 579
You might need to include the client side socket. i.e add the below code in your html file being served.
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
Integrating Socket.IO in the documentation is a good place to start.
Upvotes: 3