Browse Source

Add services

main
Haris Razis 4 years ago
parent
commit
9a09281fe8
No known key found for this signature in database GPG Key ID: 86A4D290ED03FAB4
  1. 0
      server/services/passport.js
  2. 25
      server/services/redis.js
  3. 16
      server/services/socket.js

0
server/services/passport.js

25
server/services/redis.js

@ -0,0 +1,25 @@
const redis = require('redis');
const subscriber = redis.createClient();
const publisher = redis.createClient();
let messageCount = 0;
subscriber.on('subscribe', function (channel, count) {
publisher.publish('a channel', 'a message');
publisher.publish('a channel', 'another message');
});
subscriber.on('message', function (channel, message) {
messageCount += 1;
console.log('Subscriber received message in channel \'' + channel + '\': ' + message);
if (messageCount === 2) {
subscriber.unsubscribe();
subscriber.quit();
publisher.quit();
}
});
subscriber.subscribe('a channel');

16
server/services/socket.js

@ -1,5 +1,8 @@
const socket = require('socket.io');
const chalk = require('chalk')
const chalk = require('chalk');
const {pub, sub} = require('../connections/redis_conn')
const {write, closeWrite, query} = require('../actions/influx_actions')
module.exports = (server) => {
// noinspection JSValidateTypes
@ -7,5 +10,16 @@ module.exports = (server) => {
io.on('connection', socket => {
console.log(chalk.bgWhiteBright.black.bold(`Client with id: ${chalk.bgBlack.whiteBright(socket.id)} just connected!`));
socket.on('disconnect', () => {
console.log(chalk.red('Client disconnected!'));
});
socket.on('data', (data) => {
const {measurement, pointName} = data;
pub.publish('data', measurement);
write(socket.id, measurement, pointName)
})
});
}
Loading…
Cancel
Save