The function that was provided as a parameter is not functioning as expected

I have a WsConnector class responsible for connecting to my backend application and subscribing to a WebSocket topic.

export class WsConnector {
    private stompClient: any;

    connect(onMessage: (msg) => void) {
        this.stompClient = Stomp.over(new SockJS('http://localhost:8080/ws'));
        this.stompClient.connect({}, () => {
            this.stompClient.subscribe('/topic/event', (msg: any) => {
                console.log('Msg received in WsConnector')
                onMessage(msg)
            });
        }, (err: any) => console.error(err));
    }
}

In addition, I have another class that utilizes the WsConnector class as follows:

export class SimpleClass {
    private ws = new WsConnector()

    private onMessageReceived(msg: any): void {
        console.log('Msg received in component')
    } 

    constructor() {
        this.ws.connect(() => this.onMessageReceived.bind(this))
    }
}

While I am able to receive messages with the log 'Msg received in WsConnector', I am unable to see 'Msg received in component' indicating that the messages are not being passed to this function. What could be causing this issue?

Answer №1

What a silly error.

this.connection.open(() => this.handleMessage.bind(this))

Make sure to update it to

this.connection.open(this.handleMessage.bind(this))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Cookies are not being sent by Angular 2

Currently, I am working on a project that involves frontend development using angular 2 and backend with symfony as the API. However, I am facing an issue where I need to send the PHPSESSID when making a request to symfony, but it is not happening as expec ...

Utilizing the "as" keyword for type assertion in a freshly created react application using create-react-app leads to the error message `Parsing error: Unexpected token, expected ";"`

After creating a new CRA project using yarn create react-app my-app --template typescript, I encountered an error when trying to run the development server with yarn start: src/App.tsx Line 5:24: Parsing error: Unexpected token, expected ";" ...

Is there a way to substitute the HOC with a single call and solely modify the prop?

One issue I've encountered in my project is the repetitive use of a Higher Order Component (HOC) for the header. Each time it's used, the props are set to determine whether header links should be displayed or not. My objective is to streamline th ...

Troubleshooting Angular applications in Rider

After conducting some research online, I stumbled upon a method to debug Angular apps in Rider. However, the process doesn't quite sit well with me due to its complexity for such a simple task. Thus, I am reaching out here to inquire if this approach ...

What is the best way to implement a hover delay for an element in Angular?

Here is an element I'm working with: <div (mouseenter)="enter()" (mouseleave)="leave()">Title</div> In my TypeScript file: onHover = false; enter() { this.onHover = true; // additional functionality... } leav ...

Issue with TypeScript Declaration File in NPM module functionality

Recently, I've been working on developing a package for NPM. It's essentially a JSON wrapped database concept, and it has been quite an enjoyable project so far. However, I've been facing some challenges when trying to include declarations f ...

Error: Angular encountered an issue while loading the resource. Preflight response was not successful

I am attempting to send a request to an API endpoint using Angular in order to retrieve JSON data. Check out my code snippet below: import { Component, OnInit } from '@angular/core'; import {HttpClient} from '@angular/common/http'; imp ...

What is the best way to retrieve organized data from a mat-table spanning multiple pages?

Is there a way to extract sorted data from the dataSource of a mat-table and save it as sortedData in order to export it to a CSV file? The dataSource has filters and pagination applied through this.dataSource.filterPredicate; this.dataSource.paginator = t ...

Angular 2 signal sender

I have a specific class definition for my Project: export class Project { $key: string; file: File; name: string; title: string; cat: string; url: string; progress: number; createdAt: Date = new Date(); constructor(file: File) { th ...

Encountering difficulties with implementing reactive forms in an Ionic Angular 7 project as the app.module.ts file appears to be missing

Currently, I am working on a project using Ionic Angular 7 and I am facing some challenges with implementing reactive forms. Since app.module.ts is not in Ionic Angular 7 anymore, I tried to search for solutions online. Unfortunately, when I followed the i ...

Can someone guide me on capturing an API response error using observables?

Currently working with Angular 4, my API call is functioning properly, returning the expected JSON for successful responses. However, in case of an error response, instead of receiving a JSON response as expected, I end up with a 404 HTML page (which is no ...

Check the type of the indexed value

I need help with a standard interface: interface IProps<T> { item: T; key: keyof T; } Is there a way to guarantee that item[key] is either a string or number so it can be used as an index for Record<string | number, string>? My codeba ...

Efficient management of npm dependencies versioning using Git workflow with Angular

In my Angular project, we combine multiple locally created artifacts that are published to the npm repository. Within the "dependencies" section of my app's package.json file, I include the following: "@k/e-lib": "^0.3.0", "@k/et-lib": "^0.3 ...

The error message TS2322 in MUI v5 states that the property 'fullWidth' is not found in the type 'IntrinsicAttributes & { theme: Theme; } & { children?: ReactNode; }'

As a user of MUI v5, I have implemented a straightforward FormControl as seen below. It is important to note that the property fullWidth is supported according to the official documentation. import React, { PropsWithChildren } from 'react' import ...

How to troubleshoot Props not functioning in nextjs-typescript?

I'm having trouble with props in my project and I can't seem to figure it out! Here are the three files. I'm still learning typescript but everything seems fine in the code, yet it's not working! Here is index.tsx file: const Home: ...

Angular 4 - Custom global error handler fails to capture Http errors

I have implemented a global event handler in my Angular application: import { ErrorHandler, Injectable, Injector } from '@angular/core'; import { Router } from '@angular/router'; @Injectable() export class GlobalErrorHandler implements ...

Challenge with Routing in Angular 2

I'm dealing with a route setup like this: path: 'something/:id/somethingElse' In my header.component.html, I need to include a link to this page. I tried the following approach: <a routerLink="['something',myIdThatIsDynamicO ...

Troubles arise when trying to compile Typescript and React with esbuild

I set out to create a new package and upload it to npm, starting with a demo package first. I began by using this repository as a foundation: https://github.com/wobsoriano/vite-react-tailwind-starter After that, I made updates to the build script: " ...

Utilizing Angular 2 to Pass an Argument to a Custom Pipe

I've developed a custom pipe that can accept 2 arguments, but I'm unsure of how to pass them. Here's the code for my pipe: export class TranslationPipe implements PipeTransform { private capitalize: boolean; constructor( ...

Tips for getting Angular Ivy and Angular Universal up and running together

I'm encountering a problem while attempting to incorporate Ivy + Angular Universal into my project. This issue only arises when I enable Ivy mode in Angular (disabling it allows me to successfully build my application). Below are the steps to replic ...