When a user click on a button on html page, how can I send a message to an Unity app ?

Hi,

Let’s say I have a html page (with some PHP and/or javascript code) with only one button, the page is named “Site A” for example, it is on a remote server.

And an application (Android, standalone windows or universal windows platform) made with Unity, named for example “App B”.

I would want the following behaviour :

→ When “App B” is launched, it is registered on server side (“Site A” side).
→ When someone click on the button on “Site A”, it sends a network message to all the applications “App B” currently running and previously registered.

I could use UnityWebRequest and use the polling technique : when “App B” is running, “App B” sends an UnityWebRequest to “Site A” every x seconds, yield for the response, and get the message in response.

But is there a way to do it without polling ? What I’d want is that the server (“Site A”) sends a message to every “App B” only when it is necessary (i.e. when a user click the button on “Site A”, in my example).
I think it is a little similar to “push notifications” or “server-sent events” but for an application.

Is it possible with Unity ? If it is, what is the name of the technique so that I could search for it ? (because I don’t know where to start to look for and I don’t know a lot about networking and web technologies but I already used the unity low-level networking api to make several apps communicating).

Thanks in advance for any answer. :slight_smile:

the answer is in your title, use SendMessage

Hi,

Thank you but the Unity app is not build for webGL platform, but for Android/iOS/UWP or even windows standalone platform, so I don’t think I can use Unity’s SendMessage function from the web page because it doesn’t know the instance of Unity app.

I think I have found a solution for Android/iOS : Firebase Cloud Messaging from Google seems to do exactly what I need (sending a message from a web-server to a Unity application client), but I did not find any solution for windows platform for the moment.

You could set up a PHP/MySQL server, store the messages in the database, and have clients periodically poll the web server for any new messages.

Hi Joe-Censored,

Thanks for your answer.

Yes, I already know this method as I mentionned this in my first post, however I wanted to know if there is a way to do this without polling (i.e. not having the clients periodically send request to the web-server to get the updates, so that the client can avoid to send request when there is no update).

I tested Firebase Cloud Messaging from Google and it works for Android and iOS : Configura un'app client Firebase Cloud Messaging con Unity

It is used for Push Notifications but I can also send data messages via a web-service and the client receive the message without having to poll the requests.