11

I'm trying to embed a youtube livestream chat onto a webpage on my website,

<iframe allowfullscreen="" frameborder="0" height="270" src="https://www.youtube.com/live_chat?v=hHW1oY26kxQ&embed_domain=localhost" width="480"></iframe>

I'm trying this, but the chat doesn't show up at all, if tried doing embeded domain using a real domain I own, but that doesn't work either.

Soviut
  • 88,194
  • 49
  • 192
  • 260
  • 1
    Embedding YouTube live chat works but not on mobile: https://stackoverflow.com/a/59349138/470749 – Ryan Dec 16 '19 at 00:00
  • I can confirm that 127.0.01 (sans port) worked, but I did not try localhost. Additionally, the iframe works on Ipad in Safari but not Chrome on either ios or android. – Blaine Garrett Apr 06 '20 at 22:52
  • This will work on iPad Chrome (currently) if you request the desktop version of the site instead of the mobile version. This may not be entirely desirable for all sites, however. – OdatNurd Jun 16 '20 at 07:01

5 Answers5

13

It seems to me that YouTube disabled the feature to embed a live chat on external websites, but then forgot to update the documentation. Or alternatively, there could be an unfixed bug that broke this feature.

2021 update: chat embedding works again. Uses the same syntax as before. As per a helpful comment below.

Details

The YouTube knowledge base still says that embedding a live chat iframe into an external website is still possible, using a URL like the one you posted (see here, in section "Embed Live chat").

However, when trying that and looking into the browser's console, you will see a message like this:

Refused to display 'https://www.youtube.com/live_chat?v=12345&embed_domain=example.com' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

You can also see that x-frame-options: SAMEORIGIN header in the "Network" tab of the browser's developer tools when looking at the response to the https://www.youtube.com/live_chat?… request.

This means that YouTube does not want a browser to embed this into an iframe except when embedded on youtube.com itself. (On YouTube itself, this embed code still works: when you inspect the source code of any currently streaming live video on YouTube, you will find that the live chat window there is made with that same /live_chat?… request, in an iframe.)

Additional indications that this feature has been removed:

  • The YouTube documentation mentions:

    The Live chat module only exists on the YouTube watch pages — it does not follow embedded players. (source)

    I believe that's the new part of the documentation, and the section "Embed live chat" further down is outdated.

  • This tutorial from 2016 uses the documented URL format to embed an example live chat near the bottom, and it now shows the same "Refused to display […] in a frame because it set 'X-Frame-Options' to 'sameorigin'." Assuming that this worked in 2016, something must have changed on YouTube's side.

  • This Reddit thread tells how somebody's embedded live chat suddenly stopped working in early September 2018 – so shortly before this question got asked.

Alternatives

  • You could use the YouTube Livestream API, in particular the LiveChatMessages endpoint, to get and create chat messages. There are probably open source libraries around to help with this. So far, I found this one (able to display but not create chat messages).

  • You could embed your own chat, either installed on your own server or a cloud-hosted livestream chat solution. This solution can also provide features that YouTube live chat does not provide, such as allowing anonymous visitors to post.

  • You could reverse proxy the https://www.youtube.com/live_chat?… URL, forwarding YouTube's response but with the X-Frame-Options header removed.

  • If you only need a solution for one or a few computers, you could use a browser extension to remove the X-Frame-Options header from YouTube's response. See this question.

tanius
  • 14,003
  • 3
  • 51
  • 63
  • 1
    2021 Update: Embedding youtube chat in external website seems to be working again. Critical thing is to include the `embed_domain` URL query parameter in the embed iframe URL: `?embed_domain=www.your-external-domain.com`. See documentation from this answer https://support.google.com/youtube/answer/2524549?hl=en. – asp47 Dec 02 '21 at 16:23
  • Also beware of typographic quotes around in the iframe `src` attribute. These ”. I copy/pasted from a website with these quotes, and it broke, leading me to this stackoverflow. Wrong: ``. The quotes were the problem, though, not the `X-Frame-Options` header. It worked fine once I used ASCII quotes. These ". – asp47 Dec 02 '21 at 16:28
7

This appears to have to do with the introduction of this change to iframes, at least when I had this issue.

To fix this, I would suggest the use of a script like the following:

<script>  
 let frame = document.createElement("iframe");  
 frame.referrerPolicy = "origin";  
 frame.src = "https://www.youtube.com/live_chat?v=VIDEO_ID&embed_domain=" + window.location.hostname;  
 frame.frameBorder = "0";  
 frame.id = "chat-embed";  
 let wrapper = document.getElementById("chat-embed-wrapper");  
 wrapper.appendChild(frame); 
</script>

Where chat-embed-wrapper is the parent of the iframe with the id chat-embed and VIDEO_ID (in the frame.src assignment) is the id of your target video. You'll have to modify this a little for your setup, but this is the general case solution.

Addison Crump
  • 666
  • 5
  • 19
2

Simple solution for Angular :

HTML part :

  <iframe frameborder="0" height="470" [src]="url" width="780"></iframe>

TS part :

constructor(private sanitizer: DomSanitizer) {}

ngOnInit() {
  this.url = this.sanitizer.bypassSecurityTrustResourceUrl('https://www.youtube.com/live_chat?v=[YOUR_LIVESTREAM_ID]&embed_domain=' + window.location.hostname);
}
Nitneq
  • 651
  • 10
  • 26
1

I'm not sure if you got this working in the end? We have managed to get it working on our site using this iframe, hope it helps you:

<iframe src="https://www.youtube.com/live_chat?v=YourVidCodeHere&embed_domain=www.YourDomainHere.com"width="100%" height="600"></iframe> 

I hope that helps:) PS it doesn't work on mobile (YouTube said they have removed this feature from mobile), but it does work on desktop and tab.

Best wishes.

0

I can confirm this indeed works. However, I received the same X Frame error when embed_domain had the value "example.com" even though the url of the page started with "https://www.example.com". i.e. ensure the value matches window.location.hostname. Addison Crump's workaround could work for this if you do not have canonical host redirection working.

Blaine Garrett
  • 1,286
  • 15
  • 23