R
R
RomanSS2016-12-29 15:26:28
MySQL
RomanSS, 2016-12-29 15:26:28

How to pull data for middleware in nodejs and mysql module?

Please tell me how to solve the problem. Before connecting to socket.io, you need to run your middleware that pulls data from the database and writes it to the socket, so that when the connection event occurs, the information from the database is already stored in the socket. The problem is that the request to the database is asynchronous, and there may still be no data when connecting.

var express = require('express');
var app = express();
var mysql = require('mysql');
var server = require('http').createServer(app);
var io = require('socket.io')(server);

io.use(function (socket, next) {

   // Как сделать запрос к mysql, так чтобы при коннекте к сокету они уже были прописаны в сокете?
    socket.mysql_data="значение из базы";
    next();

});

io.on('connection', function (socket) {
    // socket.mysql_data должна уже содержать данные из базы 
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-12-30
@RomanSS

Call next() in the database callback - where the data has already returned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question