Salutare,
Am urmatorul script node.js
// index.js
const express = require('express');
const cors = require('cors');
const uuid = require('uuid');
const app = express();
const fs = require('fs');
const https = require("https");
const fetch = require('node-fetch');
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended: false}));
let subscribers = [];
const httpsServer = https.createServer({
key: fs.readFileSync('xxx.key'),
cert: fs.readFileSync('yyy.crt')
}, app);
app.get('/italia_keno_10e_20_90', (request, response, next) => {
const headers = {'Content-Type': 'text/event-stream','Connection': 'keep-alive','Cache-Control': 'no-cache'};
response.writeHead(200, headers);
const subscriberId = uuid.v4();
const data = ``;
response.write(data);
const subscriber = {id: subscriberId,response};
subscribers.push(subscriber);
request.on('close', () => {
console.log(`${subscriberId} Connection closed`);
subscribers = subscribers.filter(sub => sub.id !== subscriberId);
});
fetch('EXEMPLE_URL_API_italia_keno_10e_20_90.json')
.then(res => res.json())
.then(json => {
subscribers.forEach(subscriber => subscriber.response.write(`data: ${JSON.stringify(json)}\n\n`));
})
})
app.post('/send_event_italia_keno_10e_20_90', (request, response, next) => {
const data = request.body;
fetch('EXEMPLE_URL_API_italia_keno_10e_20_90.json')
.then(res => res.json())
.then(json => {
console.log(json);
subscribers.forEach(subscriber => subscriber.response.write(`data: ${JSON.stringify(json)}\n\n`));
response.json(json);
})
})
httpsServer.listen(8080, () => {
console.log('HTTPS Server running on port 443');
});
Iar ca si client
var es = new EventSource('https://BLABLA.com:8080/italia_keno_10e_20_90');
es.addEventListener('message', function (e) {
var json_data_api_results = JSON.parse(e.data);
});
Iar cu codul php, trimit app.post catre node.js
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://BLABLA.com:8080/send_event_italia_keno_10e_20_90',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{}',
CURLOPT_HTTPHEADER => array(
'Content-Type: text/plain'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Ideea este in felul urmator, cand exista modificari in EXEMPLE_URL_API_italia_keno_10e_20_90.json, eu rulez scriptul php de mai sus, iar clientul vede in timp real modificarile cu EventSource.
Totul functioneaza normal, insa intrebarile mele sunt:
- Va merge ok pentru 30000 de utilizatori unici pe zi? Cateodata am si 2500 utilizatori in acelasi timp.
- Se mai poate optimiza ceva la cod? Daca da, ce? Nu prea am cunostiinte node, e primul script pe care l-am facut.
- Momentan rulez scriptul node din terminal, insa daca inchid terminalul se opreste si scriptul node. Ce pot face sa ruleze tot timpul la parametri normali? Adica sa nu se inchida din greseala etc.
- In codul node am mai multe app.get(‘/italia_keno_10e_20_90’ si app.post(‘/send_event_italia_keno_10e_20_90’, doar ca cu alte denumiri, este ok?
Multumesc.