Although this is an old issue, none of the suggested solutions worked for me. Therefore, I came up with a different approach that resolved the problem.
Do Not Waste your time if your socket connection polling seems to be functioning correctly without any errors. The issue might stem from a conflict between the socket.io.client package and your other configurations.
The versions of the applications in my setup are as follows:
Angular 12.x
Socket.io.client 4.x
Node 14.16.1
I also attempted using the ngx-socket-io package, but that did not provide a solution either. It's puzzling that while the socket.io polling works fine and the handshakes are correct, receiving new message events fails.
My final resolution involved manually adding socket.io to the index.html file of Angular and triggering an event to the component.
function RsCustomEvent ( event, message ) {
params = { bubbles: false, cancelable: false, detail: message };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
var socket = io('localhost:3000');
socket.on("channel-name", function (message) {
window.dispatchEvent(RsCustomEvent('socketEventListen',message));
});
Subsequently, in the Angular component, I utilized the following code:
import { Observable , fromEvent} from 'rxjs';
fromEvent(window, 'socketEventListen').subscribe((data) =>{
});
I downloaded the socket.io client JS file manually, placed it in the asset folder, and used the aforementioned codes.