Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
jameskentTX opened this issue May 13, 2018 · 16 comments
Closed
1 task done

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

jameskentTX opened this issue May 13, 2018 · 16 comments
Labels
bug Something isn't working
Milestone

Comments

@jameskentTX
Copy link

jameskentTX commented May 13, 2018

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:
angular/angular-cli#9827 (comment)

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

@darrachequesne
Copy link
Member

There are indeed a number of global references in the project and its dependencies: https://github.com/search?utf8=%E2%9C%93&q=global+repo%3Asocketio%2Fengine.io-client+repo%3Asocketio%2Fengine.io-parser+repo%3Asocketio%2Fsocket.io-client+repo%3Asocketio%2Fsocket.io-parser+extension%3A.js&type=Code&ref=advsearch&l=&l=

I'm not sure about the side-effects of removing these references though.

@jessycormier
Copy link

@darrachequesne Would it be possible to have these changes added to a fork of the project until properly resolved?

@darrachequesne
Copy link
Member

@jessycormier what do you mean by "a fork of the project"?

A first side-effect, it seems removing the global in darrachequesne/has-binary#4 breaks some Electron apps: darrachequesne/has-binary#5...

@christo-ph
Copy link

Hi. Is here any progress? Any solution? Any recommended workaround?

@datumgeek
Copy link

@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)

@LabinotAvdiu
Copy link

@realshaft thank you , it works in angular6 ;)

@iampeters
Copy link

iampeters commented Oct 19, 2018

hello @everyone,
please help me out.
so i created a service to handle my websocket connection and injected it a component.
the connection to the socket is fine as i get the a user connected in the server console.
but when i tried to send a message from the component nothing is sent.
here are my files:
socketService.ts

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import * as io from 'socket.io-client';

@Injectable({
  providedIn: 'root'
})
export class SocketService {

  private url = 'http://localhost:3000';
  private socket = io(this.url);

  // send a message to socket
  message( msg ) {
    // send msg
    this.socket.emit('chat', msg);
  }

  chat(msg) {
    this.socket$.emit('chat', msg);

    // let observable
  }
}

and the component file

import { Component, OnInit } from '@angular/core';
import { SocketService } from '../../socket.service';

@Component({
  selector: 'app-socket',
  templateUrl: './socket.component.html',
  styleUrls: ['./socket.component.scss']
})
export class SocketComponent implements OnInit {

  constructor(
    private socket: SocketService
    ) { }

  ngOnInit() {
  }

  send() {
    const msg = 'hello';
    this.socket.message(msg);
  }

}

here is my socket server connection. i used MVC

const gameModel = require('../models/game')
const userModel = require('../models/users')
const wsModel = require('../models/socket')
const socket = require('socket.io')

module.exports = (server) => {

const io = socket(server);

// WebSocket handler
io.on('connection', (socket) => {
    console.log('a user connnected')
})

// get the chat msg
io.on('chat', (msg) => {
    // io.emit('res', json.stringify(msg))
    console.log(msg);
})

}

thank you in advance

@mjarkk
Copy link

mjarkk commented Oct 24, 2018

here a bit hacky way to get it working:
Download socket.io-client from a cdn And place it in the src folder with the name socket.io-client.js

Inside the main.ts add these lines:

import * as io_ from './socket.io-client.js';
window['io'] = io_;

Inside polyfills.ts add:

(window as any).global = window;

Now you can use call socket.io-client using:

window['io']('localhost')

@pcnate
Copy link

pcnate commented Oct 26, 2018

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

@edgexie
Copy link

edgexie commented Oct 31, 2018

it help me, but why?

@sagarbhamre
Copy link

@realshaft thank you , it works in angular6 ;)

It worked for me on Angular 7.x also, thanks.

@fromage9747
Copy link

(window as any).global = window;
This worked for me as well on Angular 7.

@ibnsamy96
Copy link

same solution works with angular 8

@jusfeel
Copy link

jusfeel commented Dec 15, 2020

God Can't believe I say this. Same solution @mjarkk provided. Works in angular10! -- For me, the reason is that 2.x server doesn't work with 3.x client!

@mjarkk
Copy link

mjarkk commented Dec 15, 2020

After 3 versions this hack stil works lmao, i bet it works the same in angular 11.
But seeing this issue popup again i would now just advice to use native javascript WebSockets as they are supported in all major browsers, for a tutorial to get started: https://javascript.info/websocket.

@darrachequesne
Copy link
Member

For future readers: this issue was fixed in socket.io-client@2.2.0, there is no reference to the global object anymore.

Related commits:

Besides: an example with Angular 11 was added here: https://github.com/socketio/socket.io/tree/master/examples/angular-todomvc

Thanks!

@darrachequesne darrachequesne added the bug Something isn't working label Dec 17, 2020
@darrachequesne darrachequesne added this to the 2.2.0 milestone Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests