Screen sharing local stream not visible when doing unpublish and publish again

Dear All,
I am using Openvidu browser 2.16.0. I am publishing screen using following code.
var OV;
var session;
var publisher;
var screenShared = true;
var mySessionId = document.getElementById(“sessionId”).value;
OV = new OpenVidu();
session = OV.initSession();
session.on(“streamCreated”, function (event) {
session.subscribe(event.stream, “subscriber”);
});
getToken(mySessionId).then(token => {
session.connect(token).then(() => {
publisher = OV.initPublisher(“publisher”, { videoSource: “screen” , publishAudio: false,});
publisher.on(‘accessAllowed’, (event) => {
console.warn(‘ScreenShare: Access allowed’);
publisher.stream.getMediaStream().getVideoTracks()[0].addEventListener(‘ended’, () => {
console.log(‘User pressed the “Stop sharing” button’);
});
session.publish(publisher);
});
publisher.on(‘accessDenied’, (event) => {
console.warn(‘ScreenShare: Access Denied’);
});
})
.catch(error => {
console.log(“There was an error connecting to the session:”, error.code, error.message);
});
});

I have written toggle screen function in following way -
$("#toggleScreenShare").click( function(){
if(!screenShared){
session.publish(publisher);
}else{
session.unpublish(publisher);
}
screenShared = !screenShared;
});

My problem is that when I unpublish the screen and publish it again. The local video is not getting displayed while the remote party can see the shared screen. I could not find any error in console.

Because you don’t subscribe your local stream every time when you publish after unpunished.

Please subscribe it everytime when publish your video.

If you need further any help ping me

Thanks
Vipin

@vipin_mishra, I tried to call following code before publishing again
session.on(“streamCreated”, function (event) {
subscriber = session.subscribe(event.stream, “subscriber”);
});

But it did not work. Is there any other way to subscribe to local stream specifically ?
Meanwhile, I figured out that adding following line before publishing again does the trick for second time-

publisher.addVideoElement(videoElement); // video element is a video html element

Now if I again try to toggle screen share I get following error-

Uncaught (in promise) TypeError: streamManagerVideo.video.parentNode is null
removeAllVideos StreamManager.ts:428
removeAllVideos StreamManager.ts:420
callDefaultBehavior StreamEvent.ts:97
unpublish Session.ts:463
helloapp.js:27
jQuery 9
helloapp.js:10
jQuery 13

Use async subscriber function
I hope this will solve this issue

Thanks
Vipin

@vipin_mishra I figured out that I have call following code every time before publishing the screen
publisher = OV.initPublisher(“publisher”, { videoSource: “screen” , publishAudio: false,});
publisher.on(‘accessAllowed’, (event) => {
console.warn(‘ScreenShare: Access allowed’);
publisher.stream.getMediaStream().getVideoTracks()[0].addEventListener(‘ended’, () => {
console.log(‘User pressed the “Stop sharing” button’);
});
session.publish(publisher);
});
publisher.on(‘accessDenied’, (event) => {
console.warn(‘ScreenShare: Access Denied’);
});

Now it is working as expected.
Thanks

Your welcome
Thanks
Regards
Vipin