Skip to content

Instantly share code, notes, and snippets.

@drinchev
Created February 4, 2013 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drinchev/4709257 to your computer and use it in GitHub Desktop.
Save drinchev/4709257 to your computer and use it in GitHub Desktop.
Basic socket.io + express app
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
app.get('/', function(req, res) {
res.send('<!doctype html> \
<html> \
<head><meta charset="utf-8"></head> \
<body> \
<center>Welcome to <strong>socket.io</strong></center> \
<script src="/socket.io/socket.io.js"></script> \
<script> \
var socket = io.connect(); \
socket.emit("message", "Howdy"); \
setInterval(function () { \
socket.emit("message", "Ping"); \
}, 1000); \
</script> \
</body> \
</html>');
});
io.sockets.on('connection', function (socket) {
socket.on('message', function(msg) {
console.log(msg);
});
});
server.listen(8000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment