Search code examples
opentoktokboxvideo-conferencing

Tokbox/Opentok: How to not display published video


I'm trying to achieve a video conference between 2 users. Below code displays published video and subscribed video. I want to publish but not display the published video to the user and only display subscribed video. How to achieve that? Thanks in advance!

            var session = OT.initSession(apiKey, sessionId)
                .connect(token, function(error) {

                    var publisher = OT.initPublisher('divPublish');
                    session.publish(publisher);
                    console.log("Publishing to session1");
                });


                    var session2 = OT.initSession(apiKey, sessionId2);
                    session2.connect(token2, function(error) {
                        if (error) {
                            console.log("Error connecting: ", error.name, error.message);
                        } else {
                            console.log("Connected to the session2.");
                        }
                    });

                    session2.on("streamCreated", function(event) {
                        //var options = { width: 400, height: 300, insertMode: 'append' }
                        session2.subscribe(event.stream, 'divSubscribe');
                        console.log("Subscribing to session2");
                    });

Solution

  • You can initialise a publisher that doesn't appear on the page by providing a detached DOM element as the first argument:

    const container = document.createElement('div');
    const publisher = OT.initPublisher(container);
    

    It's up to you if you want to attach that container to the page or not.

    See targetElement argument of OT.initPublisher: https://tokbox.com/developer/sdks/js/reference/OT.html#initPublisher