Last active
July 13, 2016 18:33
-
-
Save jkachmar/fc3731784d7b6a4812cf41f96d663043 to your computer and use it in GitHub Desktop.
Mock Helium EventSource payload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "js-language-tests", | |
"version": "0.0.1", | |
"description": "Mock EventSource data", | |
"private": true, | |
"main": "server.js", | |
"license": "MIT", | |
"dependencies": { | |
"eventsource": "~0.2.1", | |
"express": "~4.13.4", | |
"sse": "~0.0.6" | |
}, | |
"engines": { | |
"node": "6.3.0", | |
"npm": "3.10.3" | |
}, | |
"scripts": { | |
"start": "node server.js" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventSource = require('eventsource'); | |
const serverList = ['http://localhost:8080/sse', | |
'http://localhost:8081/sse']; | |
serverList.forEach((addr) => { | |
new EventSource(addr) | |
.addEventListener('sensor', (e) => { | |
console.log(e.data); | |
}) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const SSE = require('sse'); | |
const app = express(); | |
const mkServer = (port) => ( | |
app.listen(port, (err) => { | |
if (err) throw err; | |
console.log(`server ready on http://localhost:${port}`); | |
}) | |
) | |
const mkSSE = (server, data) => { | |
new SSE(server).on('connection', (conn) => { | |
console.log('new connection'); | |
const pusher = setInterval(() => { | |
conn.send({ | |
event: 'sensor', | |
data: JSON.stringify(data), | |
}) | |
}, 1000); | |
conn.on('close', () => { | |
console.log('lost connection'); | |
clearInterval(pusher); | |
}); | |
}); | |
} | |
const server1 = mkServer(8080); | |
const server2 = mkServer(8081); | |
const temperature = { | |
"data": { | |
"attributes":{ | |
"value":"12.2", | |
"timestamp":"2016-11-29T00:20:20Z", | |
"port":"t" | |
}, | |
"id":"bef87974-4000-4ef3-8f39-d761cd005c92", | |
"meta":{ | |
"created":"2016-07-05T19:27:19.793892Z" | |
}, | |
"type":"data-point" | |
} | |
} | |
const pressure = { | |
"data": { | |
"attributes":{ | |
"value":"20.3", | |
"timestamp":"2016-11-29T00:20:20Z", | |
"port":"p" | |
}, | |
"id":"bef87974-4000-4ef3-8f39-d761cd005c92", | |
"meta":{ | |
"created":"2016-07-05T19:27:19.793892Z" | |
}, | |
"type":"data-point" | |
} | |
} | |
mkSSE(server1, temperature); | |
mkSSE(server2, pressure); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment