I'm currently attempting to integrate Socket.IO with NestJS. Upon loading the front end, I am noticing a high frequency of connections and disconnections to the NestJS gateway.
Below is an example of the connection/disconnection logs:
[Nest] 12232 - 01/06/2021, 11:28:24 AM [NotificationGateway] Client connected: a2f47PSPB9oUn74AACr
[Nest] 12232 - 01/06/2021, 11:28:30 AM [NotificationGateway] Client connected: NPBzcFBJLHAkQmcAACs
[Nest] 12232 - 01/06/2021, 11:28:30 AM [NotificationGateway] Client disconnected: hM8j9f9Fd6zT2HJiAACn
[Nest] 12232 - 01/06/2021, 11:28:36 AM [NotificationGateway] Client disconnected: kx6x1FMIPqfPNZa9AACo
[Nest] 12232 - 01/06/2021, 11:28:36 AM [NotificationGateway] Client connected: hHWW5iusE86GaszSAACt
[Nest] 12232 - 01/06/2021, 11:28:42 AM [NotificationGateway] Client connected: V3ah407F2uCge4GxAACu
[Nest] 12232 - 01/06/2021, 11:28:42 AM [NotificationGateway] Client disconnected: c-VEB3fnPCWGt8hlAACp
[Nest] 12232 - 01/06/2021, 11:28:48 AM [NotificationGateway] Client connected: NGuWrdwUQiKigGspAACv
The "Connected" console.log statement never seems to execute.
Here is a snippet of my simple client-side code :
<!doctype html>
<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.5/socket.io.js'></script>
<script>
const socket = io.connect('http://127.0.0.1:3000/');
socket.on('connected', function() {
console.log("connected !");
});
socket.connect();
</script>
</head>
<body>
</body>
</html>
And here is a segment from my NestJS gateway logic:
interface SockClient {
uid: string;
sock: Socket;
}
// @Injectable()
@WebSocketGateway()
export class NotificationGateway
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
constructor(
@Inject(forwardRef(() => NotificationService))
private readonly notificationService: NotificationService,
@Inject(forwardRef(() => HttpService))
private readonly httpService: HttpService,
) {}
@WebSocketServer() server: Server;
private logger: Logger = new Logger('NotificationGateway');
private clients: SockClient[] = [];
// Additional methods...
You can view a screenshot of my Chrome requests tab here.
Despite following various tutorials meticulously, I have been unable to pinpoint the root cause of the issue. Any assistance would be greatly appreciated. Thank you!