Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Created April 12, 2020 17:32
Show Gist options
  • Save stonehippo/772d9d1e72dae68401859438c858cc5c to your computer and use it in GitHub Desktop.
Save stonehippo/772d9d1e72dae68401859438c858cc5c to your computer and use it in GitHub Desktop.
A sample Node.js server for emitting events to a socket, and a simple client for receiving those events
const EventEmitter = require("events")
const Net = require("net")
const CONFIG = {
INTERVAL: 100,
DURATION: 60000,
PORT: 9050,
}
const dataPoint = () => [Math.random(), new Date()]
const emitter = new EventEmitter()
emitter.on('event', data => console.log(data))
.on('terminate', () => console.log('data source terminated'))
.on('error', err => console.error('event emitter error'))
const createServer = emitter => Net.createServer(socket => {
const onData = data => socket.write(JSON.stringify(data) + "\n")
const onTerminate = () => socket.end("Reached end of service. Disconnecting.\n")
emitter.on('event', onData)
.on('terminate', onTerminate)
socket.on('close', () => emitter.removeListener('event', onData))
.on('error', () => console.error("socket error"))
})
const server = createServer(emitter)
server.listen(CONFIG.PORT, () => {
console.log(`listening on ${server.address().address}/${server.address().port}`)
})
const terminateInMsec = (duration = 1000, emitter, server, interval) => {
return setTimeout(() => {
emitter.emit('terminate')
emitter.removeAllListeners()
clearInterval(interval)
server.close()
}, duration)
}
const emitToDataSource = emitter => {
emitter.emit('event', dataPoint())
}
// start emitting events with a cadence
let interval = setInterval(() => emitToDataSource(emitter), CONFIG.INTERVAL)
// plan to kill the stream in a few seconds
terminateInMsec(CONFIG.DURATION, emitter, interval)

Node Event Service Demo

I created a simple Node.js server that emits events, then uses a socket to publish them, as well as very basic client for listenting on that socket to consume the events.

This demo server was created as part of a POC for a broadcast system that required timestamp-based synchronization of events across multiple streams.

const Net = require('net')
const Readline = require('readline')
// set the host to the address of the server running the emitter
const CONFIG = {
PORT: 9050,
HOST: '',
BROADCAST_DELAY_SIMULATION: 1000,
}
let rl
console.log(`Trying connection to ${CONFIG.HOST}:${CONFIG.PORT}`)
const client = Net.createConnection(CONFIG.PORT, CONFIG.HOST, () => {
console.log("connected to host!")
rl = Readline.createInterface({
input: client
})
rl.on('line', (line) => {
const data = JSON.parse(line)
const value = data[0]
const timestamp = new Date(data[1])
// use a timer to simulate broadcast delay
setTimeout(() => {
const now = new Date()
console.log("server timestamp: ", timestamp)
console.log("local timestamp", now)
console.log("offset is ", now - timestamp, "ms")
}, CONFIG.BROADCAST_DELAY_SIMULATION)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment