Created
February 16, 2019 02:45
-
-
Save klogic/5c35bc46bcf6c593b49b055cc8fb8475 to your computer and use it in GitHub Desktop.
socket-io-playground/app.ts
This file contains hidden or 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 axios from 'axios'; | |
import express, { Request, Response } from 'express'; | |
import ioserver, { Socket } from 'socket.io'; | |
import ioclient from 'socket.io-client'; | |
const app = express(); | |
const server = require('http').Server(app); | |
const port = 3000; | |
const io = ioserver(server); | |
app.get('/', (req: Request, res: Response) => { | |
const socketclient = ioclient('http://localhost:' + port); | |
socketclient.on('connect', async () => { | |
for (let i = 1; i < 50; i++) { | |
const getUser = fetchUsers(socketclient, i); | |
} | |
res.json({ data: 'just hi' }); | |
}); | |
}); | |
io.on('connection', (socket: Socket) => { | |
socket.on('my other event', (data) => { | |
console.log('I got data. will running another function', data.count); | |
}); | |
}); | |
const fetchUsers = (socketclient: SocketIOClient.Socket, incrementId: number) => { | |
// fetch data and send response back to socket io. | |
const fetchUsers = axios | |
.get('https://jsonplaceholder.typicode.com/users') | |
.then((result) => { | |
socketclient.emit('my other event', { data: result.data, count: incrementId }); | |
return result.data; | |
}) | |
.catch((error) => error.message); | |
return fetchUsers; | |
}; | |
server.listen(port, () => console.log(`Example app listening on port ${port}!`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment