Search code examples
c#laravellaravel-broadcastlaravel-websockets

How can I connect to Laravel broadcasting from c# desktop app?


I have laravel chat application working through laravel broadcasting on laravel-echo-server. On front end I subscribe to channels and listen events using laravel-echo npm package, but how can I subscribe to channels in desktop app written on c# ?

I expect to have something like this written on c#:

Echo.private('SomeChannel')
    .listen('SomeEvent', (response) => {});

Solution

  • install https://github.com/doghappy/socket.io-client-csharp package

    using Newtonsoft.Json;
    using SocketIOClient.WebSocketClient;
    using SocketIOClient;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace c_sharp_demo
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                Console.OutputEncoding = Encoding.UTF8;
                Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
                 var uri = new Uri("http://localhost:6001");
    
                 var socket = new SocketIO(uri, new SocketIOOptions
                {
                    Query = new Dictionary<string, string>
                    {
                        {"token", "io" }
                    }
                });
    
                socket.OnConnected += Socket_OnConnected;
                socket.OnPing += Socket_OnPing;
                socket.OnPong += Socket_OnPong;
                socket.OnDisconnected += Socket_OnDisconnected;
                socket.OnReconnecting += Socket_OnReconnecting;
                try
                {
                    await socket.ConnectAsync();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    throw;
                }
    
                socket.On("event-name", response =>
                {
                    Console.WriteLine($"server: {response}");
                });
    
                Console.ReadLine();
            }
    
            private static async void Socket_OnConnected(object sender, EventArgs e)
            {
                Console.WriteLine("Socket_OnConnected");
                var socket = sender as SocketIO;
    
                await socket.EmitAsync("subscribe", new
                {
                   channel = "channelName",
                   auth = ""
                });
            }
    
             private static void Socket_OnPing(object sender, EventArgs e)
            {
                Console.WriteLine("Ping");
            }
    
            private static void Socket_OnPong(object sender, TimeSpan e)
            {
                Console.WriteLine("Pong: " + e.TotalMilliseconds);
            }
    
            private static void Socket_OnDisconnected(object sender, string e)
            {
                Console.WriteLine("disconnect: " + e);
            }
    
            private static void Socket_OnReconnecting(object sender, int e)
            {
                Console.WriteLine($"Reconnecting: attempt = {e}");
            }
        }
    }