Context
When casting from Google Chrome and using the session listener it's possible to join an already created session, this works well when the session is created from chrome, and if the autoJoinPolicy was set to ORIGIN_SCOPED it even works from a different tab.
The problem
If another device created a session the sessionListener handler doesn't fire, so I'm not able join the created session.
Between devices (android, ios) is possible to detect sessions, that the same behavior I would like to achieve with chrome, is this possible?
Yes, it is possible to detect sessions created by other devices in Google Chrome using the Presentation API and the session listener.
Here is an example of how you can detect and join a session created by another device:
Set up the session listener and get the current presentation connection:
const connection = navigator.presentation.getPresentationConnection(); connection.onconnectionavailable = event => { // Handle the connection event here console.log(event.connection); };
Check the connection's id and url to see if it matches the session you want to join:
if (event.connection.id === 'your-session-id' && event.connection.url === 'your-session-url') { // Join the session event.acceptConnection(); }
If you want to be able to join sessions from different origins, you need to set the autoJoinPolicy to 'always':
navigator.presentation.defaultRequest.autoJoinPolicy = 'always';
This will allow you to detect and join sessions created by other devices, regardless of their origin.
Note: Keep in mind that the Presentation API is still in development and may not be fully supported by all browsers.