Introduction
Hello Friends!!!! In this article, I am going to explain how to pass data from a WinForms application to a Web application (ReactJS) and vice versa using CefSharp browser events. CefSharp browser is Chromium-based browser control for WinForms and WPF applications. CefSharp is a .NET wrapper around the Chromium Embedded Framework and it is open source.
Demo
Simple Chat application
There are two applications,
- WinForm application (.NET Framework 4.7.2)
- ReactJS application
WinForm Application
Create a new WinForm application.

Add CefSharp.WinForms package using Nuget manager,

Create the "Message.cs" class and create a model for data.
public class Message
{
[JsonProperty("data")]
public object Data { get; set; }
}
Create a simple form as below,

Add code to create CefSharp browser.
public SampleForm()
{
InitializeComponent();
AddBrowserControl();
}
private void AddBrowserControl()
{
browser = new ChromiumWebBrowser("http://localhost:3000");
pnlBrowserContainer.Controls.Add(browser);
browser.Dock = DockStyle.Fill;
browser.JavascriptMessageReceived += OnReceiveNotification;
browser.LoadingStateChanged += OnLoadingStateChanged;
}
Add the below method and bind it to the CefSharp event handler called "LoadingStateChanged". The method will be triggered when the web application has fully loaded in the browser control. The method is used to execute the simple javascript code. In the javascript code, attach the "CefSharp.PostMessage" handler with "messageSentEvent" type. Whenever the event is dispatched with the type "messageSentEvent" in the web application then "CefSharp.PostMessage" will be executed.
private void OnLoadingStateChanged(object sender, EventArgs e)
{
string script = @"if(!messageSentEvent){
var messageSentEvent = new Event('messageSentEvent');
document.addEventListener('messageSentEvent', (message)=>{
CefSharp.PostMessage(JSON.stringify(message.detail));
});
}";
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
}
Add below the method for receiving data from a web application and bind it to the CefSharp eventhandler called "JavascriptMessageReceived". Whenever the data has been sent from a web application, then this method will be triggered.
private void OnReceiveNotification(object sender, JavascriptMessageReceivedEventArgs e)
{
Message message = JsonConvert.DeserializeObject<Message>((string)e.Message);
if (rtChatHistory.InvokeRequired)
{
rtChatHistory.Invoke(new Action(() => rtChatHistory.AppendText("Web: " + message.Data + Environment.NewLine)));
}
else
{
rtChatHistory.AppendText("Web: " + message.Data + Environment.NewLine);
}
}




Kay SPosted Nov 4, 2021, 11:33 PM
The ReactJS application is not necessary to demonstrate the communication and IMHO an overkill. simple use Browser.LoadHtml. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <label>Message:</label> <br /> <input type="text" id="message" /> <button class="btn-Send">Send</button> <br /> <br /> <label>Chat History:</label> <br /> <textarea rows="10" cols="50" id="textarea"></textarea> <br /> <script> var message = document.querySelector("#message"); var send = document.querySelector(".btn-Send"); var textarea = document.querySelector("#textarea"); send.addEventListener("click", function (e) { var chatMessage = message.value; SendMessageHandler(chatMessage); textarea.value += "\nWeb: " + chatMessage; message.value = ""; }); function ReceiveMessageHandler(e) { textarea.value += "\nWindows: " + e.detail.data; } function SendMessageHandler(e) { var event = new CustomEvent("messageSentEvent", { detail: { data: e, type: "SendMessage" } }); document.dispatchEvent(event); } document.addEventListener("messageReceiveEvent", ReceiveMessageHandler); </script> </body> </html>