# Screen sharing stopped event: Cannot set property 'session' of undefined

**URL:** <https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773>\
**Category:** Issues developing apps\
**Tags:** v2\
**Created:** [May 13, 2020, 10:58am UTC](https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773 "2020-05-13T10:58:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ertan](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ertan](https://openvidu.discourse.group/u/ertan)\
**Post date:** [May 13, 2020, 10:58am UTC](https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773/1 "2020-05-13T10:58:31Z")

</div>

I want client share webcam and screen. I can now do it but the problem I can not catch stopping screen share. I throws the `Cannot set property 'session' of undefined` error. Here is my code:

```
function shareScreen() {
    OVS = new OpenVidu();
    sessionScreen = OVS.initSession();
    var nickName = $("#nickName").val();
    getToken((token) => {
        sessionScreen.connect(token, {
                clientData: nickName + "_screen"
            })
            .then(() => {

                var publisher = OVS.initPublisherAsync("", {
                    videoSource: "screen"
                }).then(publisher => {
                    publisher.stream.getMediaStream().getVideoTracks()[0].addEventListener('ended', () => {
                        console.log('User pressed the "Stop sharing" button');
                    });
                });
                sessionScreen.publish(publisher);

            })
            .catch(error => {
                console.warn('There was an error connecting to the session:', error.code, error.message);
            });
    });
}

```

Can you tell me whats wrong with this code? Thank you.

---

<div class="post-metadata">

**Author:** ![micael.gallego](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/micael.gallego/32/2_2.png) [@micael.gallego](https://openvidu.discourse.group/u/micael.gallego)\
**Post date:** [May 13, 2020, 12:12pm UTC](https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773/2 "2020-05-13T12:12:55Z")

</div>

Can you point the source code line where you have the issue?

---

<div class="post-metadata">

**Author:** ![ertan](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ertan](https://openvidu.discourse.group/u/ertan)\
**Post date:** [May 13, 2020, 12:19pm UTC](https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773/3 "2020-05-13T12:19:17Z")

</div>

@micael.gallego,

The code line is:  
`sessionScreen.publish(publisher);`

Actually I fixed it. I moved the line inside the then function of `publisher` and it works. But now, `publisher.on('videoElementCreated', (event) => {` event does not fire in the `then` function. I move the `videoElementCreated` event outside of `then` function, I get the following error:

`There was an error connecting to the session: undefined publisher.on is not a function`

I am not good at JS so I guess, I’M missing something here.

---

<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:** [May 16, 2020, 9:09am UTC](https://openvidu.discourse.group/t/screen-sharing-stopped-event-cannot-set-property-session-of-undefined/773/4 "2020-05-16T09:09:52Z")

</div>

Try with this:

```
function shareScreen() {
    OVS = new OpenVidu();
    sessionScreen = OVS.initSession();
    var nickName = $("#nickName").val();
    getToken((token) => {
        sessionScreen.connect(token, {clientData: nickName + "_screen"}).then(() => {
			var publisher = OVS.initPublisher("", {videoSource: "screen"});

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

			});

			publisher.once('accessDenied', (event) => {
				console.warn('ScreenShare: Access Denied');
			});

		}).catch((error => {
			console.warn('There was an error connecting to the session:', error.code, error.message);

		}));
	}
}
```
