0

I am trying to solve this for hours.. if someone can help me. I cant understand why this doenst emit or receive the message.

At angular I got this error:

http://localhost:4200/socket.io/?EIO=3&transport=polling&t=NEhK-JT 404

4200 is the PORT of my Angular Application and 8080 from my server.

NodeJS:

// Define Porta
const port = process.env.PORT || 8080;
var server = app.listen(port, function () {
    console.log('Server Online - ' + port);
});

// Socket.io
var io = require('socket.io').listen(server);
io.on('connect', function (socket) {
    console.log(`${socket.id} is connected`);
    socket.on('room', function (room) {
        console.log('room', room)
        socket.join(room);
    });
});

Angular 9:

import * as io from 'socket.io-client'
public socket
public orgId: string = '123abc'

  ngOnInit(): void {
        this.setupSocketConnection();
  }

  chat(nome: string, avatar: number, mensagem: string) {
    io.connect(this.orgId).emit('organizacao', {
      nome: nome,
      mensagem: mensagem,
      avatar: avatar,
    });
  }

  setupSocketConnection() {
    this.socket = io.connect(`http://localhost:8080`, { 
      reconnectionDelay: 1000,
      reconnection: true,
      reconnectionAttempts: 10,
      transports: ['websocket'],
      agent: false, 
      upgrade: false,
      rejectUnauthorized: false
    });
  }

From my Console.log at Server

zEnR7Cp23zcur4_kAAAH is connected
86sIiMA8vRZEN-WcAAAI is connected
SU4K2n9jAx_UO2ndAAAJ is connected
UAwlMpNiZWhw_eo9AAAK is connected
K6myruVum4FPKTeLAAAL is connected
Z5QULdZtdsRo5gC1AAAM is connected

1 Answer 1

1

If you are trying to implement rooms then please read https://socket.io/docs/rooms/

For your case what I can see here is that on server-side you are listening to the event named room and on client-side you are emitting to organizacao and also you need to use the socket object instead of io, refer from here https://www.npmjs.com/package/socket.io-client

socket.connect(this.orgId).emit('room', {
      nome: nome,
      mensagem: mensagem,
      avatar: avatar,
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your help. When i change to socket, nothing happens.
have you changed organizacao to room ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.