Last active
January 17, 2022 14:02
-
-
Save erdemildiz/d9e882015e81ca933aab14e2fcb41a87 to your computer and use it in GitHub Desktop.
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 express from "express"; | |
import { createServer } from "http"; | |
import { Server } from "socket.io"; | |
import fetch from "node-fetch"; | |
const app = express(); | |
const server = createServer(app); | |
const io = new Server(server, { | |
allowEIO3: true, | |
}); | |
const apiKey = "{{ API_KEY }}"; // https://coinmarketcap.com/api/ | |
const limit = 10; | |
const currencyListUrl = `https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=${apiKey}&limit=${limit}`; | |
io.on("connection", (socket) => { | |
console.log("a user connected"); | |
fetchList(socket); | |
socket.on("get-new-currency-list", () => { | |
fetchList(socket); | |
}); | |
}); | |
const fetchList = (socket) => { | |
fetchCurrencList().then((currencyList) => { | |
socket.emit("fetched-currency-list", { list: currencyList }); | |
}); | |
}; | |
const fetchCurrencList = () => | |
new Promise((resolve, reject) => { | |
console.log("List fetched"); | |
fetch(currencyListUrl) | |
.then((res) => res.json()) | |
.then((res) => resolve(res)) | |
.catch((error) => reject(error)); | |
}); | |
server.listen(5000, () => { | |
console.log("socket run on 5000: http://localhost:5000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment