# Screen share event for subscribers

**URL:** <https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353>\
**Category:** How to implement?\
**Tags:** v2\
**Created:** [March 30, 2020, 11:58am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353 "2020-03-30T11:58:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![blazeag](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@blazeag](https://openvidu.discourse.group/u/blazeag)\
**Post date:** [March 30, 2020, 11:58am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/1 "2020-03-30T11:58:18Z")

</div>

Hi, I’m using openvidu browser, and it’s really fantastic.

Now, I would like to detect when a publisher is screensharing (noth initializing publisher with videoSource: ‘screen’ or using replaceTrack() to switch his media), so that I can manage its video differently in every subscriber’s browser (e.g. enlarging it, or whatever).

Does it exist an event fired somewhere that can help me in this?

---

<div class="post-metadata">

**Author:** ![CSantosM](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/csantosm/32/16_2.png) [@CSantosM](https://openvidu.discourse.group/u/CSantosM)\
**Post date:** [March 30, 2020, 2:18pm UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/2 "2020-03-30T14:18:12Z")

</div>

Hello @blazeag,

Listening to **steramCreated** event you will be able to receive the stream created.  
You can check the video type with stream [typeOfVideo property](https://openvidu.io/api/openvidu-browser/classes/stream.html#typeofvideo).  
This property can has three values: “CAMERA”, “SCREEN” or “CUSTOM”.

This is the simplest way to know if a screen is connected.

Regards

---

<div class="post-metadata">

**Author:** ![blazeag](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@blazeag](https://openvidu.discourse.group/u/blazeag)\
**Post date:** [March 30, 2020, 5:37pm UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/3 "2020-03-30T17:37:33Z")

</div>

> [@CSantosM](#):
>
> Listening to **steramCreated** event you will be able to receive the stream created.

Thank you @CSantosM, your solution worked well when the stream is created the first time.  
But when I use the new v2.12.0 replaceTrack() function, no streamCreated event is fired.

---

<div class="post-metadata">

**Author:** ![CSantosM](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/csantosm/32/16_2.png) [@CSantosM](https://openvidu.discourse.group/u/CSantosM)\
**Post date:** [March 30, 2020, 9:45pm UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/4 "2020-03-30T21:45:52Z")

</div>

Hello,

The replaceTrack method doesn’t fire any event so you have two options:

1. Use the traditional way to change from camera to screen (initializing a new publisher) and listen to streamCreated event.

2. Use the replaceTrack method and [send a custom signal](https://openvidu.io/api/openvidu-browser/classes/session.html#signal) whenever the method is used with the stream relevant info.

Regards.

---

<div class="post-metadata">

**Author:** ![blazeag](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@blazeag](https://openvidu.discourse.group/u/blazeag)\
**Post date:** [March 30, 2020, 10:53pm UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/5 "2020-03-30T22:53:13Z")

</div>

I like replaceTrack() very much, so I don’t want to get rid of it XD

Using signal is definitely a good idea, thank you very much @CSantosM

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [March 31, 2020, 9:42am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/6 "2020-03-31T09:42:28Z")

</div>

We can study the possibility of triggering [streamPropertyChangedEvent](https://openvidu.io/api/openvidu-browser/classes/streampropertychangedevent.html) when a Publisher calls replaceTrack.

---

<div class="post-metadata">

**Author:** ![blazeag](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@blazeag](https://openvidu.discourse.group/u/blazeag)\
**Post date:** [March 31, 2020, 2:36pm UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/7 "2020-03-31T14:36:19Z")

</div>

That would be superb @pabloFuente, thank you. Meanwhile, using signal for this purpose is working like a charm @CSantosM, being aware of the possible security issues (not a real problem in my environment, anyway)

---

<div class="post-metadata">

**Author:** ![Somnath\_Pal](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/somnath_pal/32/237_2.png) [@Somnath\_Pal](https://openvidu.discourse.group/u/Somnath_Pal)\
**Post date:** [April 10, 2020, 6:39am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/8 "2020-04-10T06:39:29Z")

</div>

Hello @CSantosM @blazeag,

I tried to listen to streamCreated but it didn’t helped. I used the below code to listen

```
     this.session.on('streamCreated', (event: StreamEvent) => {
      const subscriber: Subscriber = this.session.subscribe(event.stream, undefined);
      this.subscribers.push(subscriber);

      event.stream.getMediaStream().getVideoTracks()[0].addEventListener('ended', () => {
        console.log('User pressed the "Stop sharing" button');
      });
    });

```

I also posted it [SO](https://stackoverflow.com/questions/61121594/openvidu-screen-share-stop-eventlistener-not-working) but got no response.

---

<div class="post-metadata">

**Author:** ![blazeag](https://avatars.discourse-cdn.com/v4/letter/b/a698b9/32.png) [@blazeag](https://openvidu.discourse.group/u/blazeag)\
**Post date:** [April 10, 2020, 8:18am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/9 "2020-04-10T08:18:40Z")

</div>

I’m using this code and it works with all browsers I tested:

```
subscriber.stream.getMediaStream().addEventListener('inactive', screen_stop);
subscriber.stream.getMediaStream().getVideoTracks()[0].addEventListener('ended', screen_stop);

```

I don’t remeber why I used two different kind of event, but be sure it helps in some way.

---

<div class="post-metadata">

**Author:** ![CSantosM](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/csantosm/32/16_2.png) [@CSantosM](https://openvidu.discourse.group/u/CSantosM)\
**Post date:** [April 10, 2020, 8:34am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/10 "2020-04-10T08:34:25Z")

</div>

I just tested it and it’s working.

---

<div class="post-metadata">

**Author:** ![Somnath\_Pal](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/somnath_pal/32/237_2.png) [@Somnath\_Pal](https://openvidu.discourse.group/u/Somnath_Pal)\
**Post date:** [April 10, 2020, 8:45am UTC](https://openvidu.discourse.group/t/screen-share-event-for-subscribers/353/11 "2020-04-10T08:45:49Z")

</div>

Actually its not working in my side. But now I got it working. I used the official doc’s method to share screen.
