Jump to content

Web Socket Question


VicMoore

Recommended Posts

VicMoore

I am trying to learn how to use the Emby Web Socket API.  I have studied the ApiClient and have tried to use the Web Socket based methods.  I need example code, like a plugin that uses Web Sockets to monitor Emby activity, or just a code snippet that shows the basics.

Vic

Link to comment
Share on other sites

VicMoore

@Luke,  I want to learn how to use this feature to observe what's going on inside Emby, such as watching all of the sessions, etc. My motivation is curiosity.  I got as far as the code below.  I could not figure out how to use the ApiClient to receive messages. I tried various things, but nothing worked.

Vic

if (ApiClient.isWebSocketSupported()) {
            console.log("[[[[[[[[[[ Web Socket Supported ]]]]]]]]]]");

            if (ApiClient.isWebSocketOpen()) {
                
                console.log("[[[[[[[[[[ Web Socket Already Open ]]]]]]]]]]");

                //ApiClient.sendWebSocketMessage("Playstate", "");


                //ApiClient.onWebSocketMessage("Playstate").then((msg) => {
                //    console.log(">>>>> Playstate Msg = ",msg);
                //});


            } else {
                console.log("[[[[[[[[[[ Open Web Socket ]]]]]]]]]]");
                ApiClient.openWebSocket();
            };

}

Edited by VicMoore
Link to comment
Share on other sites

VicMoore

@Luke For example, I would like to see all of the sessions, rather than polling for the data.

ws.send(json.dumps({"MessageType":"SessionsStart", "Data": "0,1500"}))

and this when I get a PlaybackStopped message:

ws.send(json.dumps({"MessageType":"SessionsStop", "Data": ""}))

 

I assume that you must first send to the ws what you want to listen to. Then you set up a listener to monitor what the ws is sending to you.  I don't know how to do either via the ApiClient.

Vic

 

Edited by VicMoore
Link to comment
Share on other sites

VicMoore

Web Socket

 
Luke edited this page on Sep 22, 2020 · 52 revisions

Clients are recommended to connect to the server's web socket in order to receive notifications of various system events, which can help reduce the need to repeatedly poll for data through the http api.

The web socket is also the preferred protocol from which to receive remote control commands.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

@LukeI have written a JS program that monitors my Emby servers and displays what's playing on all of my TV's, and thereby allows me to send commands to each TV to control it.  It's something I did just for fun and to learn how to do it.  It all runs on a tablet hanging on the wall. After writing the code, I read, as noted above, that the preferred way to get all of this information is via the Emby Web Socket, rather than polling.  So I tried to use the ApiClient to receive information according to the preferred method.  This motivated my question to you.  It may be that what I read is no longer true.  

Vic

 

Link to comment
Share on other sites

  • 2 weeks later...

OK, we need to better document this. So yes you can do this, but until we have such documentation in place you may just have to sniff what the web app is doing using the browser debugger and mimic that.

Link to comment
Share on other sites

mickle026

@VicMooredid you get anywhere with sockets?

I am interested because I would like a way for my plugins to communitate with the webpage, to update whateve I wanted to send.  Websockets look the way to go, but they seem overcomplicated.  I would like my plugin to send data to be recieved by the webpage and update as recieved.

I dont know how to use them in Emby

 

As an alternative, i have been looking for ways to send updaetes from the internals to my page (I know its not sockets, but still a viable answer)

I am actually thinking along the lines of posting data from my c# app or having some sort of receiver at the webpage and updating on the weburl page

If i could do this, the you can internally use whatever you want to get whatever data you want and post it to the page without the page needing to request it.

I think with your skillset you would know more about this than me

 

An exmple I found on microsofts site would be to just write data to the url

public static void UploadString(string address)
{
    string data = "Time = 12:00am temperature = 50";
    WebClient client = new WebClient();
    // Optionally specify an encoding for uploading and downloading strings.
    client.Encoding = System.Text.Encoding.UTF8;
    // Upload the data.
    string reply = client.UploadString(address, data);
    // Display the server's response.
    Console.WriteLine(reply);
}

..Then using javascipt (another example I found for the page)

response = client.UploadString(URL, "POST", myItemsObject);

Of course you cound json serialise/deserialise

But i dont have an idea yet how javascript could detect the message reciept.........

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...