Skip to content

Instantly share code, notes, and snippets.

@erdemildiz
Last active January 17, 2022 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erdemildiz/d9e882015e81ca933aab14e2fcb41a87 to your computer and use it in GitHub Desktop.
Save erdemildiz/d9e882015e81ca933aab14e2fcb41a87 to your computer and use it in GitHub Desktop.
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