6.2 Setting Up socket.io Server-Side
So far we've created an Express server. Now we want to start building a real-time Q&A moderation service and we've decided to use socket.io.
Task 1:
Using the
Task 2:
Using the
Task 3:
Use the object stored in
Task 4:
When a new client connects, log a message using
Task 5:
Finally, we want to tell our
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client){ console.log(client + "has connected."); }); server.listen(8080); ```
6.3 Client socket.io Setup
In our
Task 1:
Load the
Task 2:
Using the global
```js ```
6.4 Listening For Questions
In our client below, listen for 'question' events from the server and call the
Task 1:
First, listen for 'question' events from the
Task 2:
Now, have the event callback function call the
```js ```
6.5 Broadcasting Questions
When a question is submitted to our server, we want to broadcast it out to all the connected clients so they can have a chance to answer it.
Task 1:
In the server, listen for 'question' events from clients.
Task 2:
Now, emit the 'question' event on all the other clients connected, passing them the question data.
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client) { console.log("Client connected..."); client.on('question', function(question){ client.broadcast.emit('question', question); }); }); server.listen(8080); ```
6.6 Saving Client Data
In our real-time Q&A app, we want to allow each client only one question at a time, but how do we enforce this rule? We can use socket.io's ability to save data on the client, so whenever a question is asked, we first want to check the
Task 1:
First, when a client emits a 'question' event, we want to set the value of
Task 2:
Second, when a client emits a 'question' event, we want to broadcast that question to the other clients.
Task 3:
Finally, when a client emits a 'question' event, check to make sure
```js var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); io.on('connection', function(client) { console.log("Client connected..."); client.on('question', function(question) { if(!client.question_asked){ client.question_asked = true; client.broadcast.emit('question', question); } }); }); server.listen(8080); ```
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.
Task 1:
With the
Task 2:
Now,
```js 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); ```
6.8 Answering Question Client
Now on the client, listen for the 'answer' event and then broadcast both the question and the answer to the connected clients.
Task 1:
Listen for the 'answer' event off of the
Task 2:
Call the
```js ```
Hey That was a great read, very informative, though native java debugging is not something I am good at i really liked this article, Check this out for a little more info.
回覆刪除Also don't forget to check out this one: Node JS Development Company
Nice content here you see more content about this topic nodejs development company
回覆刪除