Skip to content

Commit

Permalink
Merge pull request #11 from smartcontractkit/fix-clstats-node-count
Browse files Browse the repository at this point in the history
Add separate socketio namespace for clnode
rupurt authored Mar 19, 2019
2 parents ddc7f5c + e3b1a37 commit d35904e
Showing 2 changed files with 27 additions and 22 deletions.
18 changes: 11 additions & 7 deletions client/src/components/ConnectedNodes.tsx
Original file line number Diff line number Diff line change
@@ -6,25 +6,29 @@ type State = {
count?: number
}

const showCount = (count?: number): string => {
if (count === undefined) {
return '...'
}
return count.toString()
}

class ConnectedNodes extends Component<Props, State> {
constructor(props: Props) {
super(props)
this.state = {}
}

componentDidMount() {
const socket = io('/')
socket.on('connectionCount', (count: number) => {
const socket = io('/', { path: '/client' })

socket.on('clnodeCount', (count: number) => {
this.setState({ count: count })
})
}

render() {
return (
<div>
Connected Nodes: {this.state.count || '...'}
</div>
)
return <div>Connected Nodes: {showCount(this.state.count)}</div>
}
}

31 changes: 16 additions & 15 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import express from 'express'
import http from "http"
import socketio from "socket.io";
import { JobRun } from "./entity/JobRun"
import socketio, { Socket } from "socket.io";
import { Connection } from "typeorm"
import { JobRun } from "./entity/JobRun"

const PORT = process.env.SERVER_PORT || 8080
const CLNODE_COUNT_EVENT = 'clnodeCount'

const server = (dbConnection: Connection) => {
let connections = 0
let clnodeCount = 0

const app = express()
app.set("port", PORT);

const server = new http.Server(app)
const io = socketio(server)

server.listen(PORT, () => {
console.log(`server started, listening on port ${PORT}`)
})
@@ -23,22 +22,24 @@ const server = (dbConnection: Connection) => {

app.get('/api/v1/job_runs', async (req, res) => {
const jobRuns = await dbConnection.manager.find(JobRun)

return res.send(jobRuns)
})

io.on('connection', (socket) => {
connections = connections + 1
console.log(`websocket connected, total connections: ${connections}`);
const statsclientio: socketio.Server = socketio(server, { path: '/client' })
statsclientio.on('connection', (socket: Socket) => {
socket.emit(CLNODE_COUNT_EVENT, clnodeCount)
})

socket.emit('connectionCount', connections)
socket.broadcast.emit('connectionCount', connections)
const clnodeio: socketio.Server = socketio(server, { path: '/clnode' })
clnodeio.on('connection', (socket: Socket) => {
clnodeCount = clnodeCount + 1
console.log(`websocket connected, total chainlink nodes connected: ${clnodeCount}`);
statsclientio.emit(CLNODE_COUNT_EVENT, clnodeCount)

socket.on('disconnect', () => {
connections = connections - 1
console.log(`websocket disconnected, total connections: ${connections}`);

socket.broadcast.emit('connectionCount', connections)
clnodeCount = clnodeCount - 1
console.log(`websocket disconnected, total chainlink nodes connected: ${clnodeCount}`);
statsclientio.emit(CLNODE_COUNT_EVENT, clnodeCount)
})
})
}

0 comments on commit d35904e

Please sign in to comment.