Sending the id from a component to a service in Angular

In my current project, I am working on a chat application using Angular with WebSocket integration. Let me provide an overview of the architecture I have designed for this project. I created a module called 'chatting' which contains a list of users. When I select a user from this list, another component named 'message' opens up.

Now, my goal is to send the user's id along with the message to the backend using a service. The user's id is obtained from the URL parameter using this.route.snapshot.params['id'] in the message.ts file.

My question: How can I pass this id to the service.ts file?

Note: I have attempted to implement this functionality using various WebSocket methods but it has not been successful.

------ My Attempt ------

message.component.ts

this.idUser = this.route.snapshot.params['id'];
sendMessage(event: any, avatar: string, idUser:Number) {
    let obj: Message = {
      text: event.message,
      avatar: avatar,
      username: this.username,
      idUser: this.idUser
    } ;
console.log("id set in message:", obj.idUser)
    this.chatService.sendMessage(obj);
  }

service.ts

idUser: Number = 0;
initializeWebSocketConnection() {
   
    const serverUrl = 'http://localhost:8020/chat-websocket/' + this.idUser;
    console.log("id in service:", serverUrl)
    const ws = new SockJS(serverUrl);
    this.stompClient = Stomp.over(ws);
    const that = this;

    this.stompClient.connect({}, function(frame: any) {
      let message: any;
      that.stompClient.subscribe('/chat/messages', function (message: any) {
        if (message.body) {
          const obj = JSON.parse(message.body);
          that.addMessage(obj.text, obj.username, obj.avatar, obj.idUser);
        }
      });
    });
  }

  addMessage(message: any, username: string, avatar: string, idUser: Number) {
    this.messages.push({
      text: message,
      date: new Date(),
      user: {
        name: username,
        avatar: avatar,
        idUser: idUser
      }
    });
  }

  // Send a chat message using stomp client
  sendMessage(msg: Message) {
    this.stompClient.send('/app/sendmsg', {}, JSON.stringify(msg));
  }}

interface IMessage {

  text: string;
  date: Date;
  user: IUser;
}

interface IUser {
  idUser: Number;
  name: string;
  avatar: string;
}

Answer №1

For detailed information on routing with parameters, please refer to the following link: https://angular.io/guide/router#link-parameters-array

To Configure:

{ path: 'yourURL/:id', component: YourComponent }

Make Active:

<a [routerLink]="['yourURL', yourId]"></a>

Resulting URL:

http://localhost:4200/yourURL/1

Retrieve ID:

this.route.snapshot.snapshot.get('id');

navigation.component.ts

Below is the code snippet for navigating with parameters:

this.route.navigate(['yourURL, this.yourId]);

If you are working in a template, follow the code below:

<a [routerLink]="['yourURL', yourId]"></a>

message.component.ts

In the message component, make sure to retrieve the parameter ID as follows:

constructor(private route: ActivatedRoute,private chatService:ChatService) {}

  ngOnInit(): void {
    this.route.paramMap.pipe(
    switchMap(params => {
    this.idUser = Number(params.get('id'));
   })
  );
}
   

After obtaining the ID, pass it on to service.ts using this.idUser:

 this.chatService.sendMessage(this.idUser);

Answer №2

To determine the setup method, consider whether you are utilizing the service directly or through a store (for example: or https://rxjs.dev). If opting for direct websocket service usage, inject it in the constructor and invoke the method upon initializing the message component. You may need a listener on the router to monitor changes. On the other hand, if using a store, create a Subject (https://rxjs.dev/guide/subject) within the websocket and subscribe reactively to ensure new data is forwarded to the backend.

User id inclusion occurs within the message passing; if this id differs from, say, a chat id and is essential for establishing a connection, then adjust your service to incorporate a setup method enabling id passage for closing and reopening with the updated id.


interface Message {
  text: string;
  date: Date;
  user: User;
}

interface User {
  id: number;
  name: string;
  avatar: string;
}

export class MessageComponent implements OnInit {
  userId = 0;
  username = '';

  constructor(
    private route: ActivatedRoute,
    private service: Service
  ) {
  }

  ngOnInit(): void {
    this.userId = this.route.snapshot.params.id;
  }

  sendMessage(event: any, message: string, avatar: string): void {
    console.log('id send message:', this.userId);

    this.service.sendMessage({
      text: message,
      date: new Date(),
      user: {
        id: this.userId, // refactor in your backend userId or just id
        name: this.username,
        avatar,
      }
    });
  }
}



@Injectable({
  providedIn: 'root'
})
export class Service {
  readonly SERVER_URL = 'http://localhost:8020/chat-websocket/'; // fixme: use window.
  sender: Subject<Message> = new Subject<Message>();
  messages: Message[] = [];

  stompClient: any;

  constructor() {
    this.initializeWebSocketConnection();
  }

  initializeWebSocketConnection(): void {
    // I don't know what initial setup you required just using your code
    this.stompClient = Stomp.over(new SockJS(serverUrl));

    this.stompClient.connect({}, (frame: any) => {

      // subscriber for new messages you want to send, user id is passed here.
      this.sender.subscribe(
        next => this.stompClient.send(next.user.id + '/app/sendmsg', {}, JSON.stringify(next))
      );

      this.stompClient.subscribe('/chat/messages', (message: any) => {
        // fixme: you probably could use a mapper here
        if (message.body) {
          const obj = JSON.parse(message.body);
          this.addMessage(obj.text, obj.username, obj.avatar, obj.idUser);
        }
      });
    });
  }

  addMessage(message: any, username: string, avatar: string, userId: number): void {
    this.messages.push({
      text: message,
      date: new Date(), // fixme: backend should set timestamps
      user: {
        id: userId,
        name: username,
        avatar
      }
    });
  }

  // Send a chat message using stomp client
  sendMessage(message: Message): void {
    this.sender.next(message);
  }
}


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

Using Angular 4 Query Parameters without Route Parameters

When using Route Params like :id, the following code works: { path: 'qp/:id?repo=1', component: QueryParamsComponent } <li><a [routerLink]="['/qp', 5]" [queryParams]="{repo:1}">Query Params</a></li> Ho ...

Listening for Backdrop Click in Angular NgBootstrap Modal

Whenever I open the modal, a form will appear. If the user starts filling out the form but accidentally clicks on the backdrop, the default action is for the modal to close. However, I am interested in capturing this event because I want to display a new ...

Issues encountered with the `npm install` command in Azure build pipelines for a simple Angular application

Transferred the GitHub Repository mentioned in this link to Azure Repository. Established the Build Pipeline using Classic Editor, and you can find the YAML Code below: trigger: branches: include: - refs/heads/master jobs: - job: Job_1 display ...

Acquiring an element through ViewChild() within Angular

I am in need of a table element that is located within a modal. Below is the HTML code for the modal and my attempt to access the data table, which is utilizing primeng. <ng-template #industryModal> <div class="modal-body"> <h4>{{&a ...

Attempting to publish and install a unique angular2 component using NPM and Angular-CLI results in successful compilation only on the initial try

I am facing a peculiar and frustrating issue. The problem revolves around an Ng2 component I have developed called via-date-picker. My goal is to publish it on NPM so that it can be easily utilized in other projects. To achieve this, I converted it into a ...

agm-circle has such a high drag sensitivity in angular 4

I have implemented an agm-circle in an agm-map within Angular 4. Everything is working as expected, however, I am experiencing an issue with the speed at which the circle moves when dragged. Is there a way to slow down this movement? Below is my code sni ...

Best practices for transferring objects between components while navigating routes in Angular

I need some advice on handling a specific component in my project. Here is the code snippet: <div class="animal"> <a routerLink="/animal/{{animal.id}}"> <div> ... </div> </a> </div> This component receives ...

Obtaining the component instance ('this') from a template

Imagine we are in a situation where we need to connect a child component property to the component instance itself within a template: <child-component parent="???"></child-component1> Is there a solution for this without having to create a sp ...

Need help with npm installation woes? In search of tips on ensuring version compatibility for Angular V16?

I've been facing numerous challenges with npm installation recently and could really use some guidance. Whenever I attempt to execute npm install, I consistently encounter version compatibility errors that disrupt my development process. It's bec ...

If the value is null, pass it as is; if it is not null, convert it to a date using the

I am currently facing an issue regarding passing a date value into the rrule plugin of a fullCalendar. Here is the snippet of code in question: Endate = null; rrule: { freq: "Daily", interval: 1, dtstart: StartDate.toDate ...

What is the best way to shorten text in Angular?

I am looking to display smaller text on my website. I have considered creating a custom pipe to truncate strings, but in my situation it's not applicable. Here's what I'm dealing with: <p [innerHTML]="aboutUs"></p> Due to t ...

Can Angular 9 be used to compile a latex document?

Is it possible to utilize Angular 9 to compile and generate PDF files using latex? Specifically, I am curious about how to compile a document using Angular and Pdflatex. The idea is for the client to input their data in the form of a JSON data structure ...

Plugin for managing network connectivity in Ionic framework

In order to check if internet and id connection are available, I need to make a server request. I have implemented the Ionic Native Network Plugin following their official documentation. Here is my code snippet: import { Component } from '@angular/c ...

Tips for incorporating runtime configuration into an Angular module and effectively leveraging it

After setting up Apollo Angular, I encountered a challenge in src/app/graphql.module.ts src/app/graphql.module.ts import { NgModule } from '@angular/core'; import { APOLLO_OPTIONS } from 'apollo-angular'; import { ApolloClientOptions, I ...

Comparing RxJS and Async/Await: Which one is the better choice?

As an Angular/Ionic developer, I recently encountered challenges with the Async/Await pattern, prompting me to reconsider my approach. A colleague suggested using the RxJS library to handle asynchronous calls more effectively, so I delved into exploring al ...

An error in typescript involving a "const" assertion and a string array

Currently, I am diving into the world of Typescript along with React. However, an error has emerged in my path that I can't seem to figure out. It's puzzling why this issue is occurring in the first place. Allow me to elaborate below. const color ...

Are MobX Observables interconnected with RxJS ones in any way?

Is the usage of RxJs observables in Angular comparable to that in React and MobX? I'm struggling to find information on this topic. ...

Issue: The observer's callback function is not being triggered when utilizing the rxjs interval

Here is a method that I am using: export class PeriodicData { public checkForSthPeriodically(): Subscription { return Observable.interval(10000) .subscribe(() => { console.log('I AM CHECKING'); this.getData(); }); } ...

Exploring TypeScript: Implementing a runtime data mapping in place of an interface

Take a look at this code snippet that defines two command handlers for a server: import { plainToClass } from "class-transformer"; enum Command { COMMAND_1, COMMAND_2, } class Command1Data { foo1!: string } class Command2Data { foo2!: ...

Integrate TypeScript into the current project

As a newcomer to Typescript, I am currently exploring the option of integrating it into my current project. In our MVC project, we have a single file that houses the definitions of all model objects. This file is downloaded to the client when the user fir ...