Created
July 4, 2013 16:33
-
-
Save arisetyo/5928974 to your computer and use it in GitHub Desktop.
Basic chat using Node, Express, Socket.io, & Knockout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta name="author" content="Galenic"> | |
<script src="js/knockout-2.1.0.js"></script> | |
<link rel="stylesheet" href="pure-min.css"> | |
<link rel="stylesheet" href="custom.css"> | |
<title>Node Showcase</title> | |
</head> | |
<body> | |
<div class="pure-u-1"> | |
<div class="form"> | |
<form data-bind="submit: sendinput" class="pure-form"> | |
<fieldset><input type="text" data-bind="value: myinput" placeholder="enter message"/></fieldset> | |
</form> | |
</div> | |
<div data-bind="foreach: messages"> | |
<div class="message"><span data-bind="text: message" class="white"></span></div> | |
</div> | |
</div> | |
<script src="/socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
function MainViewModel(data) { | |
var self = this; | |
var socket = io.connect('http://localhost:8070'); | |
self.messages = ko.observableArray([]); | |
self.myinput = ko.observable(); | |
socket.on('pushdata', function (data) { | |
self.messages(data); | |
}); | |
self.sendinput = function(){ | |
socket.emit('input', { 'message': self.myinput() }); | |
} | |
} | |
var vm = new MainViewModel(); | |
ko.applyBindings(vm); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var application_root = __dirname, | |
express = require('express'), | |
path = require("path"); | |
app = express(); | |
var server = require('http').createServer(app), | |
io = require('socket.io').listen(server); | |
app.configure(function () { | |
app.use(express.logger('dev')); | |
app.use(express.bodyParser()); | |
app.use(express.static(path.join(application_root, "public"))); | |
}); | |
var messagesArray = [{ 'message':'welcome to Node showcase' }]; | |
io.sockets.on('connection', function (socket) { | |
socket.emit('pushdata', messagesArray); | |
socket.on('input', function (data) { | |
messagesArray.push(data); | |
io.sockets.emit('pushdata', messagesArray); | |
}); | |
socket.on('disconnect', function () { | |
io.sockets.emit('user disconnected'); | |
}); | |
}); | |
server.listen(8070); | |
console.log('Node Showcase is listening on port 8070...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment