The Client

The Client object is used to fetch data from LiveMarket API and supplementing it with methods to perform actions on the entities. You can obtain some information about the client or change it’s behaviour at runtime.

Client options

You can customize the default behavior of LiveMarket client with the following options.

import { Client, FetchClientAdapter } from 'livemarket-sdk';

const options = {
    // (required) Client token.
    token: '',
    // (required) Client adapter - used to make GraphQL requests.
    adapter: new FetchClientAdapter(),
    // (optional) Pusher library - required for certain live update features.
    pusher: Pusher,
    // (optional) App key for pusher client.
    pusherKey: '',
    // (optional) 2-letter language code of the language in which the data will be requested in. (defaults to 'EN')
    language: 'EN',
    // (optional) Emit events when a new video is published or a video is going live. Requires Pusher to be provided.
    liveUpdates: true
};

const client = new Client(options);

Learn more about Client Adapters.

User information

When you make your first call to the LiveMarket API using the client, a TemporaryUser is creaeted for you. You can get it’s information with by referring to the user property. If you haven’t made any calls to the LiveMarket API, the user property will be null.

const user = client.getUser();

// Contents of the user object.
{
    email: "[email protected]",
    id: "1234",
    nick: "temp-user",
    temporaryToken: "vh92u89nsaifbn"
}

Set user’s nickname

You can set user’s nickname at any time using setUserNick.

await client.setUserNick('Joe');

You can also set user’s nick along with sending a chat message. See sendChatMessage.

Change client’s language

You can change the language of the requested data using setLanguage.

client.setLanguage('UA');

Get current language

Get the language currently used by the client.

const language = client.getLanguage();

Events

If Pusher is provided - the client can emit a few events about the state of videos on LiveMarket. You can add and remove listeners from the client through the eventBroadcaster property.

client.eventBroadcaster.addEventListener('video-published', ({ id }) => {
    console.log(`A new video has been published! It\'s ID is ${id}`);
});

client.eventBroadcaster.addEventListener('video-live-started', ({ id }) => {
    console.log(`A video is going live! It\'s ID is ${id}`);
});

Note: You can enable these events after initializing the client with enableLiveUpdates() method.