Reputation: 705
Using socket.io the client hangs up (page keeps loading) on posting a form - stopping the node server the client runs fine again instantly.
Server
app.post('/', function(req, res) {
io.sockets.emit('messages', {
user: req.body.user,
message: req.body.message
});
});
Client
<script src="socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('messages', function (data) {
console.log(data);
$('.entry').first().before(
'<div class="entry well well-large">'
+ data.user
+ ' says: '
+ data.message
+ '</div>');
});
Upvotes: 0
Views: 1908
Reputation: 203514
You should always end an HTTP request, but you're not doing that in your post
handler. If you don't, Express won't return an answer to your browser and your browser will keep waiting for one (until at some point it gets tired of waiting and generates a timeout).
Try this instead:
app.post('/', function(req, res) {
io.sockets.emit('messages', {
user: req.body.user,
message: req.body.message
});
// end the request by ending the response
res.end();
});
(it's not enough to send a message with socket.io
, because that's a separate protocol)
Upvotes: 4