Search code examples
ruby-on-railsangularserver-side-renderingangular-universalactioncable

Angular Universal/SSR --> ERROR TypeError: ActionCable.WebSocket is not a constructor


Using Actioncable js for front end as web sockets with rails on the server side, everything else works fine, while doing Angular SSR, it builds perfectly. But when I hit a url it says

ERROR TypeError: ActionCable.WebSocket is not a constructor

I've already done: (global as any).WebSocket = require('ws'); and installed ws


Solution

  • I finally figured it out! While doing Server Side Rendering, I was connecting to the websockets before the page was reaching the browser, hence it couldn't make the connection.

    Angular has a way of detecting platform browser https://angular.io/api/common/isPlatformBrowser

    Just used it to detect if the code is in the browser and then make the socket connection.