Skip to content

Instantly share code, notes, and snippets.

@pjcodesjs
Last active May 6, 2021 05:06
Show Gist options
  • Save pjcodesjs/cca2baea753417b258fc3e25b8713331 to your computer and use it in GitHub Desktop.
Save pjcodesjs/cca2baea753417b258fc3e25b8713331 to your computer and use it in GitHub Desktop.
Socket.io Setup with Express JS
// 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