question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Library socket.io-client is not compatible with latest Angular 6

See original GitHub issue

I want to:

  • report a bug - solved, look last two lines for workaround

Library socket.io-client is not compatible with latest Angular 6. There is error at execution time:

ReferenceError: global is not defined at Object…/node_modules/socket.io-client/node_modules/socket.io-parser/is-buffer.js (is-buffer.js:4)

Please read comment below to know more: https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814

If you want to see this error for yourself, add to angular 6.0 app simple service like that below:

import { Injectable } from 'angular/core';
import * as socketIo from 'socket.io-client';

@Injectable()
export class SocketService {
    private socket;

    constructor() {}

    public initSocket(): void {
        this.socket = socketIo();
    }
}

Error disappears when you remove initSocket function.



Ok, there is workaround in Angular 6 to work with socket.io-client. Add (window as any).global = window; to polyfills.ts

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:43
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
pcnatecommented, Jan 10, 2019

mine started working by simply adding the following

Inside polyfills.ts add:

(window as any).global = window;

angular 6.1.0 socket.io-client 2.1.1

This continues to work after upgrading to angular 7.1.x

14reactions
datumgeekcommented, Aug 17, 2018

@realshaft - the above work-around worked for me in Ionic 4 / Angular 6 web app

Ok, there is workaround in Angular 6 to work with socket.io-client. Add (window as any).global = window; to polyfills.ts (located in src folder)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client Installation | Socket.IO
The Socket.IO client is compatible with bundlers like webpack or browserify. NPM; Yarn; pnpm. npm install socket.io-client
Read more >
Angular 6 and Socket.IO - Socket.On not working
I'm trying to use socket.io with an Angular 6 project and it doesn't seem to be working. Here is my issue: I have...
Read more >
socket.io-client - npm
Realtime application framework client. Latest version: 4.5.4, last published: a month ago. Start using socket.io-client in your project by ...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
Step 1 — Setting Up the Project Directory and Creating the Socket Server · Step 2 — Installing @angular/cli and Creating the Client...
Read more >
How to Integrate Socket.IO with Angular 2021 - Ultimate Akash
npm install -g @angular/cli · ng new client //client is project name · cd client npm i ngx-socket-io · import { NgModule }...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found