Reputation: 786
this is my index.html
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>
<ul id="messages"></ul>
<script>
var socket = io();
socket.on('testerEvent', function(data){
console.log(data)
});
</script>
</body>
</html>
this is my index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.emit('testerEvent', { description: 'A custom event named testerEvent!'});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
In index.html listener not working on client side but working on server side. I don't know what's wrong.
Upvotes: 0
Views: 1561
Reputation: 262
It is not listening to any events because you are not telling it from where to listen to. try this:
var connectionOptions = {
"force new connection": true,
"reconnectionAttempts": "infinity",
"timeout": 10000,
"transports": ["websocket"]
};
const socket = io(/*your server*/, connectionOptions);
Upvotes: 1