1

I want to pass GET value from URL to socket.io so it can emit to front-end. I have this:

app.get('/data/', function (req, res) {
  var queryData = url.parse(req.url, true).query;
  res.send('Temp: '+queryData.temp);
  var temp = queryData.temp; //<= this value I want to send
}); 

and this:

io.sockets.on('connection', function (socket) {
socket.emit('message', temp); //<= here I want to put data
});

Idea is: make GET requests with some value (example.com/data/?temp=22), pass that value to index.html in real-time (so I can put in graph).

EDIT: I have semi-working solution:

io.sockets.on('connection', function (socket) {

    app.get('/data/', function (req, res) {

      var queryData = url.parse(req.url, true).query;
      res.send('Temp: '+queryData.temp);
      var temp = queryData.temp;

    socket.emit('message', temp);

    }); //app.get data
});

This working form time to time, not always. Sometime works great, sometime not at all. But it is what I want... only to work always.

Stfnsn
  • 141
  • 1
  • 12
  • The websocket connection and the http connection are independent. You can't give a websocket response to the same client that made the http request without some logic to identify the socket client that made each get request. Usually this logic involves dealing with express signed cookie, which is shared by both connections. – Miguel Calderón Mar 07 '18 at 11:47
  • @MiguelCalderón take look at my new code. But, beside that, only solution are cookies? Maybe you have different idea how to send data to server and show on front-end (without DB, in real time)? – Stfnsn Mar 07 '18 at 12:00
  • You don't need a DB or any other intermediate storage / messaging service as long as you're not clustering your app (which is the first thing you do when you want to scale it up). Keeping it simple then (with only one process), you can get use of the solution showed here, which does use the session cookie: https://stackoverflow.com/questions/25532692/how-to-share-sessions-with-socket-io-1-x-and-express-4-x – Miguel Calderón Mar 07 '18 at 12:05
  • Why do you need to send the query over a socket connection anyway? Client can access it via `location.query` – Patrick Roberts Mar 07 '18 at 19:13
  • @PatrickRoberts I have assignment to make some kind live-data server->client app. This idea first came to my mind and I thought I would, probably, know how to make it. I'm web designer, so... :) – Stfnsn Mar 08 '18 at 15:48

1 Answers1

0

Ok, I found solution (and my mistake). So, I just have to use socket.broadcast.emit instead of socket.emit Final code server-side:

     io.sockets.on('connection', function (socket) {
            app.get('/data/', function (req, res) {
              var queryData = url.parse(req.url, true).query;
              res.send('Temp: '+queryData.temp);
              var temp = queryData.temp;
//                     ⬐ added
            socket.broadcast.emit('message', temp);
            });
        });

Client side:

socket.on('message', function(message) {
          $("#data ul").append('<li>' +message+'</li>');
})
Stfnsn
  • 141
  • 1
  • 12