Query about socket.io and pub sub redis [closed]

2

I'm doing a bot using socket.io and pub sub with redis (I know it's not necessary but I'm using it for other things as well), the problem is that it bounces repeated data.

This is the script that controls the sending of data

chat.js

 $scope.sendMensaje=function(){
        $scope.mensaje=getMessageText();
        sendMessage($scope.mensaje);
        $http({
            method:"POST",
            url:"/chat",
            data:{data:$scope.mensaje},
            headers:{
                "Content-Type":"application/json"
            }
        })
        .then(function(response,headers,config){

            console.log("Exito",headers,config);

        })
        .catch(function(response){
            console.log("Error",response);
        })

    };

And here where you receive

chat.js

var socket=io.connect("http://localhost:9001");

socket.on("new message",function(data){
    console.log("Mensaje recibido",data)
    setTimeout(function(){
                sendMessage(JSON.parse(data).data);

    },1000);
});

// sendMessage what it does is print the message in the chat

On the server side endpoint / chat

app.post("/chat",(req,res)=>{
  console.log("Data de input",req.fields.data);

conversation.message(
{
  input: { text:req.fields.data }
},processResponse);
//redisClient.publish("chat",JSON.stringify(req.fields));
});

processResponse.js

var redisClient=require("../redisClient");

module.exports=function processResponse(err, response) {
  if (err) {
    console.log("Que error era",err)
    //console.error(err); // something went wrong
  }
  else{
      console.log(response);

  // Display the output from dialog, if any.
  if (response.output.text.length != 0) {
    //console.log("Recibe de watson",response.output.text[0]);
    redisClient.publish("chat",JSON.stringify({data:response.output.text[0]}));
  }
  }
}

Now I'm doing some tests, and I've put a middleware that when loading a page under any endpoint / user, sends publishes a content, with publish.

middlewareInitConversation

var ConversationV1 = require('watson-developer-cloud/conversation/v1');

var watsonConfig=require("../config/watson.conversation");

var conversation = new ConversationV1(watsonConfig);




module.exports=function(req,res,next){
console.log("MiddlewareInitConversation")

var processResponse=require("../auxiliares/processResponse");
conversation.message({}, processResponse);
 next();

}

route user.js

var express=require("express");
var router=express.Router();
var user=require("../models/user")
//////////
//////////
router.use(middlewareInitConversation);
////
router.route("/login").get(logged.unlogged,cUser.login_get).post(logged.unlogged,cUser.login_post);
////

realtime.js

module.exports=(server,sessionMiddleware)=>{
    var io=require("socket.io")(server);
    var redis=require("redis");
    var client=redis.createClient();

    client.subscribe("chat");

    io.use((socket,next)=>{
        sessionMiddleware(socket.request,socket.request.res,next);
        console.log("Esto esta aca",socket.request);
    });

    client.on("message",(channel,mensaje)=>{
        if(channel=="chat"){
            console.log("Mensaje recibido en realtime")
            io.emit("new message",mensaje);
        }

    });

    io.sockets.on("connection",(socket)=>{

        //console.log("Session de socket en middleware",socket);
    });
}

Then the logic is as follows, when I enter any endpoint under / user, a query is made to the watson api, to return the start message, then it is sent by publish to the defined in realtime, the data under the chat channel, and in the client the message is received and displayed.

But I see that it is repeated as you see in the image,

I think it's because of these lines, which are the ones related to the socket, it hardly loads the page, there are clearly 2 queries to the same ID, maybe that's why it throws twice the same result?, but what I do not understand where Do those 2 questions come out?

http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtby XHR finished loading: GET "http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtby".
http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtcX&sid=no5jigx9xnRMqMvdAAAC XHR finished loading: GET "http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtcX&sid=no5jigx9xnRMqMvdAAAC".
http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtgP&sid=no5jigx9xnRMqMvdAAAC XHR finished loading: GET "http://localhost:9001/socket.io/?EIO=3&transport=polling&t=LcdMtgP&sid=no5jigx9xnRMqMvdAAAC".
    
asked by Kevin AB 16.01.2017 в 14:17
source

0 answers