Reputation: 404
I am new to Node js,developing a chatting application referring to example given in http://socket.io/get-started/chat/. In this chatting application client(browser) sends requests to the server and gets connected if succeeded. Server code is given below
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('chat.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Client code: chat.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome to chat</title>
<script>
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', {reconnect: true});
socket.on('connect', function(socket) {
console.log('Connected!');
});
</script>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="on" /><button>Send</button>
</form>
</body>
</html>
Now I get the message 'listening on *:3000' when I run the application(using eclipse) and point my browser to localhost:3000, I get the chat.html page , however I never get the message 'a user connected'. I mean client here is not connecting to server socket and hence I am not getting the above message displayed in console. I tried everything I can do. Any suggestions will be of great help. Thanks in advance.
Upvotes: 0
Views: 102
Reputation: 2873
You can't use require
on frontend
var io = require('socket.io-client');
Use
<script src="/socket.io/socket.io.js"></script>
Upvotes: 1