Skip to content

Instantly share code, notes, and snippets.

@IgorMuzyka
Last active August 29, 2015 13:56
Show Gist options
  • Save IgorMuzyka/9250677 to your computer and use it in GitHub Desktop.
Save IgorMuzyka/9250677 to your computer and use it in GitHub Desktop.
node.js socket.io chat in 44 lines
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>
</head>
<body>
<input id="messageToSend" type="" />
<input type="button" value="send" onclick="send()" />
<br /><br /><br />
<p id="talk">
</p>
<script type="text/javascript">
var messageToSend = document.querySelector("#messageToSend");
var talk = document.querySelector("#talk");
var chat = io.connect("http://localhost:8080/chat");
chat.on("connect", function() {
console.log("connected");
chat.emit("message", "hi");
});
chat.on("message", function(message) {
newText = JSON.stringify(message).split("\"")[1];
talk.innerHTML += "<br />" + newText;
});
var send = function() {
chat.emit("message", messageToSend.value);
messageToSend.value = "";
}
</script>
</body>
</html>
io = require "socket.io" .listen 8080
chat = io.of "/chat" .on "connection", (socket) ->
socket.on "message", (message) ->
chat.emit "message", "#{socket.id} says: #{message}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment