Reputation: 7737
I seem to have socket.io setup and trying to emit some tweets, but they're not appearing in the browser. This is what I'm getting in the console:
GET / 304 16ms
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized LYlpc7dcpLyHCFcKYUJy
debug - setting request GET /socket.io/1/websocket/LYlpc7dcpLyHCFcKYUJy
debug - set heartbeat interval for client LYlpc7dcpLyHCFcKYUJy
debug - client authorized for
debug - websocket writing 1::
debug - websocket writing 5:::{"name":"tweets","args":[{"user":"AaronNewport97","text":"You're not your","latitude":51.56764399,"longitude":0.49722754}]}
51.56764399 0.49722754
Here's the relevant code from app.js:
if(data.geo!=null){
latitude = data.geo.coordinates[0];
longitude = data.geo.coordinates[1];
io.sockets.volatile.emit('tweets', {
user: data.user.screen_name,
text: data.text,
latitude: latitude,
longitude: longitude
});
}
And this is what I've got on the html side:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
jQuery(function ($) {
var tweetList = $('ul.tweets');
socket.on('tweet', function (data) {
tweetList .prepend('<li>' + data.user + ': ' + data.text + '</li>');
});
});
</script>
In the broswer console, I've got:
Request URL:ws://localhost:3000/socket.io/1/websocket/vBFWuusMIQbO9PeJZv8p
Request Method:GET
Status Code:101 Switching Protocols
And it looks like the connection is pending. Why is it doing this?
Upvotes: 1
Views: 658
Reputation: 12161
Change this line
io.sockets.volatile.emit('tweets', {
for this
socket.volatile.emit('tweet', {
Upvotes: 0
Reputation: 203304
The message you send is named tweets
, but your browser is waiting for a message named tweet
(without the -s
).
Upvotes: 3