{"id":3578,"date":"2024-03-09T09:25:40","date_gmt":"2024-03-09T15:25:40","guid":{"rendered":"https:\/\/promincproductions.com\/blog\/?p=3578"},"modified":"2024-03-09T09:25:41","modified_gmt":"2024-03-09T15:25:41","slug":"youtube-live-stream-and-live-chat-embedded-in-website-responsive","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/youtube-live-stream-and-live-chat-embedded-in-website-responsive\/","title":{"rendered":"YouTube Live Stream and Live Chat Embedded in Website (Responsive)"},"content":{"rendered":"<p>This tutorial shows how to embed a YouTube live stream <code>iframe<\/code> into an HMTL page <strong>with<\/strong> the live chat streaming as well.  While the YouTube platform is nice, it can be nice to keep visitors on your domain while watching a live stream to show extra promotions, etc.<\/p>\n\n\n\n<p>Embedding YouTube videos (and chat for that matter) can be tricky in that it is done via the <code>iframe<\/code> element.  <code>iframe<\/code>&#8216;s are not the most flexible HTML element.  This is partially due to the nature of what is happening &#8211; <em>blindly<\/em> embedding the contents of one site into another.  The parent site doesn&#8217;t know what the dimensions of the child site is, should it scroll or not, etc.  While they can be frustrating to work with, it is possible to accomplish the desired user experience in this scenario &#8211; including be responsive, providing a favorable experience for mobile users.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"600\" height=\"600\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed.png\" alt=\"YouTube Embed iframe Live Video and Chat Responsive\" class=\"wp-image-3589\" title=\"YouTube Embed iframe Live Video and Chat Responsive\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed-500x500.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed-150x150.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed-450x450.png 450w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">How to embed YouTube Live Video and Chat into a Website<\/h2>\n\n\n\n<p>Embedding a YouTube Live video and it&#8217;s accompanying Live Chat is done by implementing two <code>iframe<\/code>&#8216;s in the website &#8211; one for the YouTube Live Video and the other for the accompanying Live Chat stream.  By using two <code>iframe<\/code>&#8216;s it allows for greater control over the layout and user experience.  Also, it&#8217;s the way that YouTube designed to work &#8211; even how they do it on their own site &#8211; yes YouTube uses an <code>iframe<\/code> to add the chat to their own site.<\/p>\n\n\n\n<p>With the two <code>iframe<\/code> elements on the page, some CSS does the styling magic.  In the case of this tutorial, it will be a 70\/30 split.  The video will be 70% of the width of the screen and the live chat will be the remaining 30%.  The CSS is responsive and so on a narrower screen (860px and less) the live chat goes below the live video <code>iframe<\/code>.<\/p>\n\n\n\n<p>One critical note here is that this CSS was written to work for videos having a 16&#215;9 aspect ratio.  It is possible to adjust the CSS for other aspect ratios as needed.  There are 2 <code>aspect-ratio<\/code> directives that would need to be adjusted.  The rest of the CSS is based on percentages and thus should work without any adjustment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing Embedded YouTube Live Video and Chat into a Website<\/h2>\n\n\n\n<p>Below is a HTML and CSS example on how to embed YouTube Live video and chat in a website.<\/p>\n\n\n\n<p>This example screenshot can be found here: <a href=\"https:\/\/promincproductions.com\/blog\/code\/youtube-live-embed-with-chat\/iframe-embed-youtube-live-with-chat.html\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"987\">Example of YouTube Live video and chat embed iframes<\/a> (NOTE: the YouTube video embedded likely will change, but the HTML and CSS are still valid even if the video and chat don&#8217;t load)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"469\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-1024x469.png\" alt=\"YouTube Live Chat with Live Stream iframe example screenshot\" class=\"wp-image-3586\" title=\"YouTube Live Chat with Live Stream iframe example screenshot\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-1024x469.png 1024w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-500x229.png 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-768x352.png 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-150x69.png 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-600x275.png 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3-450x206.png 450w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/image-3.png 1267w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">YouTube Embed HTML<\/h3>\n\n\n\n<p>This HTML has the YouTube <code>iframe<\/code> elements wrapped in the container HTML structure that allows for styling of the elements.<\/p>\n\n\n\n<p>There are two parts to this that need to be modified specific to the video being embedded.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">YouTube ID<\/h4>\n\n\n\n<p>Replace the two entries of <code>&lt;youtube_id&gt;<\/code> with the YouTube ID of the video to be embedded.  This is most easily obtained from the URL of the live stream YouTube page from the <code>v<\/code> query parameter.  This needs to be set on both <code>iframe<\/code>&#8216;s.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">embed_domain Query Parameter<\/h4>\n\n\n\n<p>To get the Live Chat to stream correctly the <code>embed_domain<\/code> query parameter for the live chat must be set to the domain (TLD) that the <code>iframe<\/code> is being added to.  So replace <code>www.example.com<\/code> with <code>www.yourdomain.com<\/code> accordingly when embedding this into the site.  If this is not set correctly, the Live Chat will not work correctly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"ytContainer ytContainerVideo\"&gt;\n    &lt;!-- YouTube Live Video --&gt;\n    &lt;iframe src=\"https:\/\/www.youtube.com\/embed\/&lt;youtube_id&gt;\" title=\"YouTube Live video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen&gt;&lt;\/iframe&gt;\n&lt;\/div&gt;\n\n&lt;div class=\"ytContainer ytContainerChat\"&gt;\n    &lt;!-- YouTube Live Chat --&gt;\n    &lt;iframe src=\"https:\/\/www.youtube.com\/live_chat?v=&lt;youtube_id&gt;&amp;embed_domain=www.example.com\"&gt;&lt;\/iframe&gt;\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">YouTube Embed CSS<\/h3>\n\n\n\n<p>This CSS is pretty straightforward and will work for videos in the 16 x 9 aspect ratio as is.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.ytContainer {\n    display:inline-block;\n    position: relative;\n}\n.ytContainer &gt; iframe {\n    border: 0;\n    width: 100%;\n    height: 100%;\n}\n@media only screen and (min-width: 860px) {\n    .ytContainer.ytContainerChat &gt; iframe,\n    .ytContainer.ytContainerChat {\n        aspect-ratio: 9 \/ 12.05;\n    }\n    .ytContainer.ytContainerVideo &gt; iframe,\n    .ytContainer.ytContainerVideo {\n        aspect-ratio: 16 \/ 9;\n    }\n}\n.ytContainerVideo {\n    width: 69%;\n}\n.ytContainerChat {\n    width: 29%;\n}\n@media only screen and (max-width: 860px) {\n    .ytContainer {\n        display: block;\n        width: 100%;\n        padding-top: 56.25%;\n    }\n    .ytContainer &gt; iframe {\n        position: absolute;\n        top: 0;\n        left: 0;\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Notes<\/h2>\n\n\n\n<p>The YouTube live chat documentation referenced below states that it will not work on mobile devices.  In testing however it works as expected using Safari and Chrome on an iPhone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/support.google.com\/youtube\/answer\/2524549?hl=en#zippy=%2Cembed-live-chat\" target=\"_blank\" rel=\"nofollow noopener\" data-lasso-id=\"988\">YouTube How to Embed Live Chat Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/promincproductions.com\/blog\/code\/youtube-live-embed-with-chat\/iframe-embed-youtube-live-with-chat.html\" target=\"_blank\" rel=\"noopener\" data-lasso-id=\"989\">Example of YouTube Live video and chat embed iframes<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to embed a YouTube live stream iframe into an HMTL page with the live [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3589,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[48,25,5],"tags":[525,524],"class_list":["post-3578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","category-responsive-website","category-website-development","tag-css","tag-youtube"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2024\/03\/youtube-iframe-live-video-and-chat-embed.png","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-VI","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/3578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=3578"}],"version-history":[{"count":6,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/3578\/revisions"}],"predecessor-version":[{"id":3590,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/3578\/revisions\/3590"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/3589"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=3578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=3578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=3578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}