socketio / socket.io-client

Realtime application framework (client)
https://socket.io
10.62k stars 3.04k forks source link

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

Closed jameskentTX closed 3 years ago

jameskentTX commented 6 years ago

I want to:

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

darrachequesne commented 6 years ago

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 commented 6 years ago

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

darrachequesne commented 6 years ago

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

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

christo-ph commented 6 years ago

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

datumgeek commented 6 years ago

@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 commented 6 years ago

@realshaft thank you , it works in angular6 ;)

iampeters commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

it help me, but why?

sagarbhamre commented 5 years ago

@realshaft thank you , it works in angular6 ;)

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

fromage9747 commented 5 years ago

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

ibnsamy96 commented 4 years ago

same solution works with angular 8

jusfeel commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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!