File

src/lib/services/chat-service.ts

Description

ChatService is your main API for using ngx-chat. Can be injected in your services like in the following example:

constructor(@Inject(CHAT_SERVICE_TOKEN) chatService: ChatService)

Index

Properties
Methods

Methods

addContact
addContact(identifier: string)

Adds the given contact to the user roster. Will send a subscription request to the contact.

Parameters :
Name Type Optional Description
identifier string No

The ID of the contact.

Returns : void
getContactById
getContactById(id: string)

Returns the contact with the given ID or undefined if no contact with the given ID is found. In case of XMPP it does not have to be bare, the search will convert it to a bare JID.

Parameters :
Name Type Optional Description
id string No

The ID of the contact.

Either the Contact or undefined.

getOrCreateContactById
getOrCreateContactById(id: string)

Always returns a contact with the given ID. If no contact exists, a new one is created and announced via contacts$. In case of XMPP it does not have to be bare, the search will convert it to a bare JID.

Parameters :
Name Type Optional Description
id string No

The ID of the contact.

Returns : Contact

The new contact instance.

getPlugin
getPlugin(constructor)
Type parameters :
  • T

Returns the plugin instance for the given constructor

Parameters :
Name Optional Description
constructor No

The plugin constructor, e.g. RosterPlugin

Returns : T
loadCompleteHistory
loadCompleteHistory()

Requests all archived messages for all contacts from the server.

Returns : Promise<void>
logIn
logIn(logInRequest: LogInRequest)

Logs the user in. Will modify state$ accordingly. If login fails, state will stay in 'disconnected'.

Parameters :
Name Type Optional
logInRequest LogInRequest No
Returns : void
logOut
logOut()

Disconnects from the server, clears contacts$, sets state$ to 'disconnected'.

Returns : void
reconnect
reconnect()

Tries to reconnect with the same credentials the user logged in last.

Returns : void
reconnectSilently
reconnectSilently()

Tries to transparently (= without the user noticing) reconnect to the chat server.

Returns : void
reloadContacts
reloadContacts()

Forces asynchronous reloading of your roster list from the server, contacts$ will reflect this.

Returns : void
removeContact
removeContact(identifier: string)

Removes the given contact from the user roster. Will cancel a presence subscription from the user to the contact and will retract accepted subscriptions from the contact to the user.

Parameters :
Name Type Optional Description
identifier string No

The ID of the contact.

Returns : void
sendMessage
sendMessage(recipient: Recipient, body: string)

Sends a given message to a given contact.

Parameters :
Name Type Optional Description
recipient Recipient No

The recipient to which the message shall be sent.

body string No

The message content.

Returns : void

Properties

blockedContacts$
blockedContacts$: Observable<Contact[]>
Type : Observable<Contact[]>

A list of contacts which the current user has blocked.

chatActions
chatActions: ChatAction<literal type>[]
Type : ChatAction<literal type>[]

The actions visible to users near to chat inputs, e.g. the send message button. Customize it for branding or to add new actions, e.g. for file uploads.

contactRequestsReceived$
contactRequestsReceived$: Observable<Contact[]>
Type : Observable<Contact[]>

A list of contacts which have sent the user a subscription request.

contactRequestsSent$
contactRequestsSent$: Observable<Contact[]>
Type : Observable<Contact[]>

A list of contacts to which a subscription from the user is outstanding.

contacts$
contacts$: BehaviorSubject<Contact[]>
Type : BehaviorSubject<Contact[]>

A BehaviorSubject of all known contacts. Contains for example Contacts that sent you a message or blocked contacts. This does not represent your roster list.

contactsSubscribed$
contactsSubscribed$: Observable<Contact[]>
Type : Observable<Contact[]>

A list of contacts to which the current user has accepted subscriptions to.

contactsUnaffiliated$
contactsUnaffiliated$: Observable<Contact[]>
Type : Observable<Contact[]>

A list of contacts where the user is not subscribed to and neither a pending request is incoming or outgoing.

enableDebugging
enableDebugging: boolean
Type : boolean

If set to true, debug information will be visible in the roster list.

message$
message$: Observable<Contact>
Type : Observable<Contact>

Will emit the corresponding contact when a new message arrive.

notBlockedContacts$
notBlockedContacts$: Observable<Contact[]>
Type : Observable<Contact[]>

contacts$ without the blockedContacts$.

state$
state$: BehaviorSubject<ConnectionStates>
Type : BehaviorSubject<ConnectionStates>

Lifecycle state machine. Starts in the state "disconnected". When logging in, the state will change to "connecting". Plugins may now initialize, for example load the contact list or request archived messages. When all plugins have completed the initialization, the new state will be 'online'.

translations
translations: Translations
Type : Translations

The current translation. Do NOT write to this attribute, use the translations @Input-attribute of ChatComponent instead.

userAvatar$
userAvatar$: BehaviorSubject<string>
Type : BehaviorSubject<string>

The avatar of the user. Is used as src attribute of an img-element. Purely cosmetical. Should be set via the userAvatar$ @Input-attribute of ChatComponent.

import {InjectionToken} from '@angular/core';
import {BehaviorSubject, Observable} from 'rxjs';
import {Contact} from '../core/contact';
import {LogInRequest} from '../core/log-in-request';
import {ChatPlugin} from '../core/plugin';
import {Recipient} from '../core/recipient';
import {Translations} from '../core/translations';
import {ChatAction} from './adapters/xmpp/xmpp-chat-adapter.service';

/**
 * The chat service token gives you access to the main chat api and is implemented by default with an XMPP adapter,
 * you can always reuse the api and ui with a new service implementing the ChatService interface and providing the
 * said implementation with the token
 */
export const CHAT_SERVICE_TOKEN = new InjectionToken<ChatService>('ngxChatService');


export type ConnectionStates = 'disconnected' | 'connecting' | 'online';

/**
 * ChatService is your main API for using ngx-chat. Can be injected in your services like in the following example:
 *
 * ```
 * constructor(@Inject(CHAT_SERVICE_TOKEN) chatService: ChatService)
 * ```
 */
export interface ChatService {

    /**
     * Will emit the corresponding contact when a new message arrive.
     */
    message$: Observable<Contact>;

    /**
     * Lifecycle state machine. Starts in the state "disconnected". When logging in, the state will change to "connecting".
     * Plugins may now initialize, for example load the contact list or request archived messages. When all plugins have completed the
     * initialization, the new state will be 'online'.
     */
    state$: BehaviorSubject<ConnectionStates>;

    /**
     * A BehaviorSubject of all known contacts. Contains for example Contacts that sent you a message or blocked contacts.
     * This does not represent your roster list.
     */
    contacts$: BehaviorSubject<Contact[]>;

    /**
     * A list of contacts which the current user has blocked.
     */
    blockedContacts$: Observable<Contact[]>;

    /**
     * contacts$ without the blockedContacts$.
     */
    notBlockedContacts$: Observable<Contact[]>;

    /**
     * A list of contacts to which the current user has accepted subscriptions to.
     */
    contactsSubscribed$: Observable<Contact[]>;

    /**
     * A list of contacts to which a subscription from the user is outstanding.
     */
    contactRequestsSent$: Observable<Contact[]>;

    /**
     * A list of contacts which have sent the user a subscription request.
     */
    contactRequestsReceived$: Observable<Contact[]>;

    /**
     * A list of contacts where the user is not subscribed to and neither a pending request is incoming or outgoing.
     */
    contactsUnaffiliated$: Observable<Contact[]>;

    /**
     * If set to true, debug information will be visible in the roster list.
     */
    enableDebugging: boolean;

    /**
     * The avatar of the user. Is used as src attribute of an img-element. Purely cosmetical. Should be set via the
     * [userAvatar$]{@link ChatComponent#userAvatar$} @Input-attribute of {@link ChatComponent}.
     */
    userAvatar$: BehaviorSubject<string>;

    /**
     * The current translation. Do NOT write to this attribute, use the [translations]{@link ChatComponent#translations} @Input-attribute
     * of {@link ChatComponent} instead.
     */
    translations: Translations;

    /**
     * The actions visible to users near to chat inputs, e.g. the send message button. Customize it for branding or to add
     * new actions, e.g. for file uploads.
     */
    chatActions: ChatAction<{sendMessage: () => void}>[];

    /**
     * Forces asynchronous reloading of your roster list from the server, {@link contacts$} will reflect this.
     */
    reloadContacts(): void;

    /**
     * Returns the contact with the given ID or undefined if no contact with the given ID is found. In case of XMPP it does not have to be
     * bare, the search will convert it to a bare JID.
     * @param id The ID of the contact.
     * @returns Either the Contact or undefined.
     */
    getContactById(id: string): Contact | undefined;

    /**
     * Always returns a contact with the given ID. If no contact exists, a new one is created and announced via contacts$. In case of XMPP
     * it does not have to be bare, the search will convert it to a bare JID.
     * @param id The ID of the contact.
     * @returns The new contact instance.
     */
    getOrCreateContactById(id: string): Contact;

    /**
     * Adds the given contact to the user roster. Will send a subscription request to the contact.
     * @param identifier The ID of the contact.
     */
    addContact(identifier: string): void;

    /**
     * Removes the given contact from the user roster. Will cancel a presence subscription from the user to the contact and will retract
     * accepted subscriptions from the contact to the user.
     * @param identifier The ID of the contact.
     */
    removeContact(identifier: string): void;

    /**
     * Logs the user in. Will modify state$ accordingly. If login fails, state will stay in 'disconnected'.
     */
    logIn(logInRequest: LogInRequest): void;

    /**
     * Disconnects from the server, clears contacts$, sets state$ to 'disconnected'.
     */
    logOut(): void;

    /**
     * Sends a given message to a given contact.
     * @param recipient The recipient to which the message shall be sent.
     * @param body The message content.
     */
    sendMessage(recipient: Recipient, body: string): void;

    /**
     * Requests all archived messages for all contacts from the server.
     */
    loadCompleteHistory(): Promise<void>;

    /**
     * Returns the plugin instance for the given constructor
     * @param constructor The plugin constructor, e.g. {@link RosterPlugin}
     */
    getPlugin<T extends ChatPlugin>(constructor: new(...args: any[]) => T): T;

    /**
     * Tries to transparently (= without the user noticing) reconnect to the chat server.
     */
    reconnectSilently(): void;

    /**
     * Tries to reconnect with the same credentials the user logged in last.
     */
    reconnect(): void;

}

results matching ""

    No results matching ""