ngx-socket-io icon indicating copy to clipboard operation
ngx-socket-io copied to clipboard

Angular ngx-socket-io duplicates message

Open moti-malka opened this issue 4 years ago • 0 comments

When I connect to the socket event for the first time I get the number of messages from the server (python flask_socket) properly but when I close the window and do socket.disconnect() and connect again using socket.connect() I get the same messages twice for example: If for the first time I get 100 messages then after closing and reopening the window I get 200 messages. On the server side it seems to be sending the correct number of messages I think I'm not really doing something right on the client side

server.py

@socketio.on('join_log_request')
def join_log_request(containerid, sessionId):
    
    container_logs = Sdk.docker_client.logs(containerid, stream=True, timestamps=True)   
    
    while True:
        try:
            log = next(container_logs).decode("utf-8")
            socketio.emit('stream_logs_response', {'log': log,  'containerid':containerid, 'sessionId':sessionId })
            print(sessionId)
            socketio.sleep(0)

        except StopIteration:
            socketio.emit('stream_logs_response', {'log': 'CONTAINER NOT RUNNING','containerid':containerid, 'sessionId':sessionId })
            break

client.ts

containerLogs(container: Container): void {
    
    this.socket.ioSocket.connect();
    
    this.sessionId = UUID.UUID()

    console.log('Connect sessionId: ' + this.sessionId)
    
    this.logContainerContext = container;
    
    this.sidenav_logs = true;
    
    this.socket.emit('join_log_request', container.id, this.sessionId);
    
    this.socket.on('stream_logs_response', (response: any) =>{
      console.log(response.sessionId)
      if(response.sessionId == this.sessionId){
          this.logs.push(response.log)
      }  
    })
  }

moti-malka avatar Nov 11 '21 16:11 moti-malka