Skip to content

Instantly share code, notes, and snippets.

@bcomnes
Last active November 4, 2015 17:53
Show Gist options
  • Select an option

  • Save bcomnes/3b14bb8b371876860631 to your computer and use it in GitHub Desktop.

Select an option

Save bcomnes/3b14bb8b371876860631 to your computer and use it in GitHub Desktop.
socket.io example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Websockets!!!11</title>
</head>
<body>
<button name="button">Click me</button>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
var button = document.querySelector('button')
console.log(button)
button.addEventListener('click', function(e) {
console.log('button clicked')
socket.emit('input', {type: 'button'})
})
</script>
</body>
</html>
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
socket.on('input', function (data) {
console.log(data)
})
});
{
"name": "rpc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.13.3",
"socket.io": "^1.3.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment