Last active
May 6, 2021 05:06
-
-
Save pjcodesjs/cca2baea753417b258fc3e25b8713331 to your computer and use it in GitHub Desktop.
Socket.io Setup with Express 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
// IMPORT AND INITIALIZE EXPRESS | |
var express = require ('express'); | |
const fetch = require('node-fetch'); | |
const socketIo = require('socket.io'); | |
var app = express(); | |
var port = 9000; | |
// CONNECT TO SOCKET IO | |
const server = app.listen(port); | |
const io = require('socket.io')(server); | |
io.on('connection', (socket) =>{ | |
// GET ALL EARTHQUAKES | |
fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson') | |
.then(resp => resp.json()) | |
.then((json) => { | |
socket.emit('latest_quakes', json); | |
}) | |
}); // END SOCKET IO FUNCTION | |
// LISTENING ON PORT... | |
console.log(`Server listening in on port ${port}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment