Skip to content

Instantly share code, notes, and snippets.

@having-fun-coding
Last active August 29, 2015 14:21
Show Gist options
  • Save having-fun-coding/1d2ccdc0a630bc9d589e to your computer and use it in GitHub Desktop.
Save having-fun-coding/1d2ccdc0a630bc9d589e to your computer and use it in GitHub Desktop.
Real Time Web with Node.js | 6.7 Answering Questions
Clients can also answer each other's questions,
so let's build that feature by first listening for
the 'answer' event on the client, which will send us
both the question and answer, which we want to broadcast
out to the rest of the connected clients.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.sockets.on('connection', function(client) {
console.log("Client connected...");
client.on('answer', function(question, answer) {
client.broadcast.emit('answer', question, answer);
});
client.on('question', function(question) {
if(!client.question_asked) {
client.question_asked = true;
client.broadcast.emit('question', question);
}
});
});
server.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment