Last active
June 14, 2020 15:16
-
-
Save nim4n136/5041ea561ea5e1357e5ff9dfc5b23383 to your computer and use it in GitHub Desktop.
Broadcast socket io
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title></title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script> | |
</head> | |
<body> | |
<script> | |
/** | |
* End point server socket io | |
* */ | |
var socket = io("http://localhost:3000/"); | |
/** | |
* Wait notification | |
* */ | |
socket.on("notification", function (msg) { | |
console.log(msg); | |
}); | |
</script> | |
</body> | |
</html> |
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": "broadcast", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"dependencies": { | |
"axios": "^0.19.2", | |
"express": "^4.17.1", | |
"socket.io": "^2.3.0" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "MIT" | |
} |
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 app = require('express')(); | |
const http = require('http').createServer(app); | |
const io = require('socket.io')(http); | |
const axios = require('axios'); | |
const END_POINT = 'https://xxx.xx' | |
// Send broadcast every 2 second | |
setInterval(function(){ | |
axios({ | |
method: 'get', | |
url: END_POINT | |
}).then(function (response) { | |
console.log("Broadcast notification ...") | |
// Sending notification | |
io.emit('notification',response.data) | |
}); | |
}, 2000) | |
http.listen(3000, () => { | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment