Skip to content

Commit

Permalink
feat: update a websocket live when changes are committed
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeen committed Dec 29, 2021
1 parent 8446d1d commit bada67c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export const commit = async (req, res) => {
fullNode.deleteProjectRecord(uuid, stagingRecordId);
break;
}

Project.changes.next(data.orgUid);
} else if (table === 'Unit') {
switch (action) {
case 'INSERT':
Expand All @@ -99,6 +101,7 @@ export const commit = async (req, res) => {
fullNode.deleteUnitRecord(uuid, stagingRecordId);
break;
}
Unit.changes.next(data.orgUid);
}
});
res.json({ message: 'Staging Table committed to full node' });
Expand Down
4 changes: 4 additions & 0 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
import Sequelize from 'sequelize';
import rxjs from "rxjs";

const { Model } = Sequelize;

import { sequelize } from '../database';
Expand All @@ -15,6 +17,8 @@ import ModelTypes from './projects.modeltypes.cjs';
import { optionallyPaginatedResponse } from "../../controllers/helpers.js";

class Project extends Model {
static changes = new rxjs.Subject();

static associate() {
Project.hasMany(RelatedProject);
Project.hasMany(Vintage);
Expand Down
3 changes: 3 additions & 0 deletions src/models/units/units.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { sequelize } from '../database';
import { Qualification, Vintage } from '../../models';

import ModelTypes from './units.modeltypes.cjs';
import rxjs from "rxjs";

const { Model } = Sequelize;

class Unit extends Model {
static changes = new rxjs.Subject();

static associate() {
Unit.hasOne(Vintage);

Expand Down
31 changes: 30 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,45 @@
import rootRouter from './routes';
import http from 'http';
import Debug from 'debug';
import { Server } from "socket.io";
import {Project, Unit} from "./models/index.js";

const debug = Debug('climate-warehouse:server');

const port = 3030;
const server = http.createServer(rootRouter);

server.listen(port);
const io = new Server(server);

const socketSubscriptions = {};

io.on("connection", (socket) => {
const { url } = socket.request;
let subject;

if (url.includes('project')) {
subject = Project.changes;
} else if (url.includes('unit')) {
subject = Unit.changes;
} else {
return socket.disconnect();
}

socket.on('disconnect', () => {
if (socketSubscriptions[socket.id]) {
socketSubscriptions[socket.id].unsubscribe();
}

});

socketSubscriptions[socket.id] = subject.subscribe(orgUid => socket.broadcast.emit({ orgUid }))
});

server.on('error', onError);
server.on('listening', onListening);

server.listen(port);

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
Expand Down

0 comments on commit bada67c

Please sign in to comment.