Skip to content

Instantly share code, notes, and snippets.

@dshaw
Forked from getify/browser-net-output.txt
Created October 6, 2011 19:05
Show Gist options
  • Save dshaw/1268318 to your computer and use it in GitHub Desktop.
Save dshaw/1268318 to your computer and use it in GitHub Desktop.
trouble getting socket.io to work in node
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Testing Socket.io</title></head>
<body><h1>Testing Socket.io</h1>
<p id="msg">connecting...</p>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
var socketIoEvents = ['connect', 'connecting', 'connect_failed', 'message', 'close', 'disconnect', 'reconnect', 'reconnecting', 'reconnect_failed'];
for (var i in socketIoEvents) {
var event = socketIoEvents[i];
socket.on(event, function () { console.log(event, arguments) });
}
socket.on("news", function(data, successFn) {
$("#msg").html("connected! " + JSON.stringify(data));
successFn("connected!");
});
</script>
</body>
</html>
{
"name": "getify-wepuzzleit"
, "version": "0.0.1"
, "dependencies": {
"connect" : "1.7.1"
, "socket.io" : "0.8.4"
}
, "main": "./server.js"
, "engines": { "node": "~0.4.0" }
}
web: node server.js
var connect = require('connect'),
app = connect.createServer(connect.static(__dirname)),
io = require('socket.io').listen(app),
port = process.env.PORT || 80
;
app.listen(port);
io.configure(function(){
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
// io.set('log level', 1); // reduce logging
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
console.log("connection established!");
//socket.on('my other event', function (data) {
// console.log("DATA:"+data);
//});
socket.emit('news', { hello: 'world' }, function(data) {
console.log("received:"+data);
});
});
@dshaw
Copy link
Author

dshaw commented Oct 6, 2011

npm install
sudo node server

@dshaw
Copy link
Author

dshaw commented Oct 6, 2011

100% less Connect.

@dshaw
Copy link
Author

dshaw commented Oct 7, 2011

Put back Connect. Inlined blobs of html are just painful.

Setup for Heroku. http://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment