# Video Communication LifeCycle

Before starting coding... let's walk through the main concepts, methods and classes used during a Conversation lifecycle.&#x20;

* First, each run of a client application instantiates one `UserAgent` representing the local individual,
* A `Session` must be opened with **ApiRTC**'s servers to create a `Conversation` to which individuals can join,
* In a `Conversation`, individuals can **chat**, **watch**, **listen** and/or **share** **real-time audio, video, data, screen...**,
* A `Stream` is audio, and/or video captured from sources available from browser and published on a `Conversation`.

### UserAgent

As the entry point to **ApiRTC**, `UserAgent` is instantiated using the **apiKey** associated to your **enterprise account** on [https://cloud.apirtc.com](https://cloud.apirtc.com/).

![UserAgent](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2FuU2aGPPUdUVXsgatJeJ6%2Fapirtc%20useragent?alt=media)

### Stream

When `UserAgent.createStream(options)` is called, a popup opens in the browser to request user acceptance for using media devices. The returned `Promise` resolves with an instance of `Stream`, which carries **media stream** from computer device through the browser.

The method `Stream.attachToElement(domElement)` binds the **media stream** to a DOM `<video>` element in order to display it in the web page.

![createstream](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2FS51iWTPsu6jxj2U1yeAa%2Fapirtc%20create%20stream?alt=media)

### Session

To use **ApiRTC**'s connected features, a `userAgent` must register to get a **session**. This can be done with or without authentication.

Simply using `UserAgent.register()` without authentication details registers a `userAgent` with **ApiRTC**'s servers without authentication.

![register](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2Fax43jTqoyGWOjHPb8OUF%2Fapirtc%20useragent%20register?alt=media)

The `session` grants access to connected features such as **conversations**.

### Conversation

An instance of `Conversation` is obtained from `Session.getOrCreateConversation(name)`.

![getOrCreateConversation](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2F4yCq7keRKicvVuptqTXb%2Fapirtc%20session%20getOrCreateConversation?alt=media)

### Join

Calling `Conversation.join()` makes the `userAgent` used to create the `Session` used to create this instance of `Conversation` (through it's `Session`) join the **conversation**.

![join](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2FDvPDtrFt4GJYGnaR7qXd%2Fapirtc%20conversation%20join?alt=media)

### Publish Streams

Once a **conversation** joined, a `userAgent` can publish a **stream** using `Conversation.publish(stream)` method:

![publish](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2F4vMFrG8clt6FqcjJBk4Z%2Fapirtc%20conversation%20publish?alt=media)

### Subscribe to Streams

Subscribing to peer **streams** shall be implemented as followed:

* Listen on `Conversation` `'streamListChanged'` event to get notified of **streams** published to the **conversation**. The data provided by the event is a `StreamInfo`: It gives information on **streams** published in the conversation such as the id (`StreamInfo.streamId`) of the **stream**. `StreamInfo.listEventType` property tells whether the event is triggered for and addition (value `'added'`) or a removal.
* Calling `conversation.subscribeToStream(streamId)` with the `StreamInfo.streamId` of an added remote stream makes this **conversation** subscribe to a **stream**.
* Finally, listening on `Conversation` `'streamAdded'` notifies with a remote `Stream` instance which can be attached to web page for display.

![subscribe](https://226856024-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9RTy6zMGBeXToEHSXgv7%2Fuploads%2F5594iyfHKkEvac006O9g%2Fapirtc%20conversation%20subscribe?alt=media)
