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.

unable to get server data in ngx-socket-io

See original GitHub issue

Hi, I wood like to request you to please help to solve this problem. i am using ngx-socket-io repository , angular 6. i am connect to the server but i am unable to get the data from the server. my code is

app.module.ts

import { SocketIoModule, SocketIoConfig } from 'ngx-socket-io';
const config: SocketIoConfig = { url: 'http://af098cb1.ngrok.io', options: {} };
 imports: [
    SocketIoModule.forRoot(config),
}

socket.services.ts

 constructor(private socket: Socket) { 
    }
 
getMessage() {
        return this.socket
            .fromEvent<any>("sendchat")
            .map(data =>  data.msg);
            
    }
getData(){
console.log("-----------");
this.socket.on('sendchat', function (socket) {
            console.log('User data', socket);
        });
}

Output:

it prints this ---------------------- but can not get the user data and response.and also i not get the single error message i cont understand whats going on please help out.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
seba-salavillacommented, Apr 4, 2019

You need to subscribe to the event getData

SocketServices.ts

import { map } from 'rxjs/operators'
getData() {
    return this.socket.fromEvent("sendchat").pipe(map( data => data )); // with pipe rxjs6
}

Then in your component class

Home.ts

constructor(private socketServices : SocketServices){
    this.socketServices.getData().subscribe( (data) => {
        console.log('User data', data);
    })
}

salute.

0reactions
swathibhecommented, Jul 9, 2020

We have to use subscribe

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - ngx-socket-io cannot connect to server because of ...
There (https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html) I found that issue may be solved by setting allowEIO3: true , but it didn ...
Read more >
Troubleshooting connection issues | Socket.IO
IO server may encounter a temporary failure or be restarted; the server itself may be killed as part of an autoscaling policy ...
Read more >
ngx-socket-io - npm
We need to configure SocketIoModule module using the object config of type SocketIoConfig , this object accepts two optional properties they are ...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
How To Create a Real-Time App with Socket.IO, Angular, and Node.js · Step 1 — Setting Up the Project Directory and Creating the...
Read more >
Socket.IO - Error Handling - Tutorialspoint
Socket.IO - Error Handling, We have worked on local servers until now, which will almost never give us errors related to connections, timeouts,...
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