Skip to content

Commit

Permalink
Set up websocket server, connect it to the API for triggering client …
Browse files Browse the repository at this point in the history
…updated when data on server is 100% changed.
  • Loading branch information
koyta committed Nov 27, 2018
1 parent 2656393 commit f874877
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const db = low(adapter);

const timersDb = db.get('timers');

module.exports.getAllTimers = () => timersDb.value();

module.exports.addTimer = timer => {
const newTimer = {
id: shortId.generate(),
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const WebsocketServer = require('./socket');

const app = express();
const WSS = new WebsocketServer(5001);
module.exports.WSS = WSS;

/**
* Express API routes
Expand Down
8 changes: 7 additions & 1 deletion routes/TimersRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const db = require('../db');
const { WSS } = require('../index');

router.get('/', getTimers);
router.get('/:id', getTimer);
Expand Down Expand Up @@ -34,6 +35,7 @@ function updateTimer(req, res) {
if (db.getTimerById(id)) {
db.updateTimer(timer);
res.sendStatus(200);
WSS.triggerClientsToUpdateData();
}
res.sendStatus(404);
}
Expand All @@ -48,6 +50,7 @@ function addTimer(request, response) {
const timer = { title, minutes, seconds };
db.addTimer(timer);
response.sendStatus(200);
WSS.triggerClientsToUpdateData();
}
}

Expand All @@ -59,21 +62,24 @@ function deleteTimer(request, response) {
} else if (id) {
db.deleteTimer(id);
response.sendStatus(200);
WSS.triggerClientsToUpdateData();
}
}

function deleteAllTimers(req, res) {
db.deleteAllTimers();
res.sendStatus(200);
WSS.triggerClientsToUpdateData();
}


function deleteTimerById(request, response) {
const { id } = request.params;
if (!id) {
response.sendStatus(400);
} else {
db.deleteTimer({ id });
response.sendStatus(200);
WSS.triggerClientsToUpdateData();
}
}

Expand Down
25 changes: 15 additions & 10 deletions socket.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
const WebSocket = require('ws');
const DB = require('./db');

class WebsocketServer {
constructor(port) {
this.server = new WebSocket.Server({ port });
this.server.on('connection', socket => {
socket.onmessage = message => {
console.log(`Message: ${message.data}`);
};
socket.send(JSON.stringify({hello: 'brother'}))
this.server.addListener('connection', function connection(socket) {
socket.send('Connected succesfully');

socket.on('message', function incoming(message) {
switch(message) {

}
});
});
}

this.server.on('close', listener => {
console.log(`Connection closed: ${listener}`);
triggerClientsToUpdateData() {
this.server.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(DB.getAllTimers());
}
});
this.server.on('message', message => {

})
}
}

Expand Down

0 comments on commit f874877

Please sign in to comment.