# Building a web socket like pusher using soketi.app & using it in Laravel + Laravel Echo Application

ဒီဆောင်းပါးမှာတော့ Web Socket Server တစ်ခုကို open-source ပေးထားတဲ့ [soketi.app](http://soketi.app) ကိုသုံးပီးတော့ ဘယ်လိုတည်ဆောက်ကြမလဲ ဆိုတာကို ရေးပြမှာဖြစ်ပါတယ်။ ဒီဆောင်းပါးကို မဖတ်ခင် Web Socket ဘယ်လိုအလုပ်လုပ်လဲဆိုတာရယ်၊ ဘယ်လိုသုံးရတယ်ဆိုတာရယ် ကိုတော့သိထားဖို့လိုပါလိမ့်မယ်။ ကျွန်တော်ကတော့ Laravel & Vue.js ကိုသုံးပီး ဆောက်ထားတဲ့ Sample Chat Application တစ်ခုကိုသုံးပီးတော့ usage ကို စမ်းပြပါ့မယ်။ Socket Server ကိုတော့ ubuntu ပေါ်မှာ run မှာဖြစ်ပါတယ်။

### **မိတ်ဆက်**

* Socketi က Pusher Protocol v7 ကိို implements လုပ်ထားတာဖြစ်ပီးတော့ သာမန် Web Socket Client ([websocket](https://www.npmjs.com/package/websocket)) တွေအပြင် လူသိများတဲ့ Pusher-maintained or compatiable clients တွေ အကုန်လုံးနဲ့ ချိတ်ဆက်ပီးတော့ အသုံးပြုနိုင်ပါတယ်။
    
* Performance အပိုင်းမှာလည်း uWebSocket.js သုံးထားပီးတော့ uWebSocket.js က
    
    * Fastify ထက် 8.5 ဆ ([https://unetworkingab.medium.com/100k-secure-websockets-with-raspberry-pi-4-1ba5d2127a23](https://unetworkingab.medium.com/100k-secure-websockets-with-raspberry-pi-4-1ba5d2127a23))
        
    * [Soket.io](http://Soket.io) ထက် ၁၀ ဆ ([https://unetworkingab.medium.com/serving-100k-requests-second-from-a-fanless-raspberry-pi-4-over-ethernet-fdd2c2e05a1e](https://unetworkingab.medium.com/serving-100k-requests-second-from-a-fanless-raspberry-pi-4-over-ethernet-fdd2c2e05a1e))
        
        မြန်တယ်လို့ သူ့ documentation မှာ ဖော်ပြထားပါတယ်။ 1GB and 1CPU machine တွေပေါ်မှာတောင် active connection 500 ကို အသာလေး handle လုပ်နိုင်တယ်လို့လည်း ဖော်ပြထားပါတယ်။ ဒါတွေက တော့ အတိအကျမစမ်းရသေးပေမယ့် တော်တော်ကို မြန်တာကိုတော့ သတိထားမိပါတယ်။
        
* Application တွေ အများကြီးနဲ့ ချိတ်ဆက်ဖို့အတွက်လည်း [App Management](https://docs.soketi.app/app-management/introduction) ကိုပါ support လုပ်ပေးထားပါတယ်။
    

### **လိုအပ်ချက်များ (Requirements)**

* Python 3.x
    
* GIT
    
* The `gcc` compiler and the dependencies for build
    
* Node.js LTS (14.x, 16.x, so on) is required due to uWebSockets.js build limitations.
    
* npm
    

ဘာမှ မလုပ်ရသေးခင် Package Manager (APT) ကို update အရင်လုပ်ကြပါမယ်။

```bash
sudo apt update
```

ဖော်ထားထားတဲ့ လိုအပ်ချက်(Requirements) တွေ မရှိသေးဘူးဆိုရင်တော့ အောက်က command တွေနဲ့သွင်းလို့ရပါတယ်။

```bash
# Python and dependencies
sudo apt install -y git python3 gcc build-essential
# Git
sudo apt install nodejs npm
# Node and NPM
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
```

### **Installation**

ကျွန်တော်ကတော့ configuration လုပ်ရလည်း လွယ်တဲ့ CLI နည်းကိုပဲရွေးထားပါတယ်။

1. [Soketi Installation via CLI](https://docs.soketi.app/getting-started/installation/cli-installation) မှာပြထားတဲ့ အတိုင်း installation လုပ်ကြပါမယ်။
    
    ```bash
    npm install -g @soketi/soketi
    ```
    
2. Soketi ကို configure လုပ်ဖို့လိုအပ်တဲ့ folder ဆောက်ပါမယ်။ ပီးရင် config.json ဖိုင်ကို အသစ်ဆောက်လိုက်တဲ့ folder ထဲမှာ ဆောက်ပေးပါမယ်။
    
    ```bash
    mkdir soketi && cd soketi && touch config.json
    ```
    
3. Configure လုပ်ဖို့လိုပါတယ်။ စမ်းဖို့အတွက်ကတော့ ပေးထားတဲ့ အောက်က config file ကို config.json မှာ ထည့်ပီးတော့ စမ်းလို့ရပါတယ်။ ဒီနေရာမှာတော့ Learning အတွက်ဦးတည်တာမလို့ [Array Drivers](https://docs.soketi.app/app-management/array-driver#app-level-limits) တွေကိုပဲသုံးပီးတော့ စမ်းပါ့မယ်။
    
    ```json
    {
      "debug": true,
      "port": 6001,
      "appManager.array.apps": [
        {
          "id": "app-id",
          "key": "app-key",
          "secret": "app-secret",
          "enabled": true,
          "enableClientMessages": true,
          "webhooks": [],
          "maxBackendEventsPerSecond": -1,
          "maxClientEventsPerSecond": -1,
          "maxReadRequestsPerSecond": -1,
          "maxPresenceMembersPerChannel": 100,
          "maxPresenceMemberSizeInKb": 2,
          "maxChannelNameLength": 200,
          "maxEventChannelsAtOnce": 10,
          "maxEventNameLength": 100,
          "maxEventPayloadInKb": 4,
          "maxEventBatchSize": 10
        }
      ]
    }
    ```
    
4. Config file သတ်မှတ်ပီးပီဆိုရင်တော့ Soketi Server ကို run ဖို့ အဆင်သင့်ဖြစ်ပါပီ။ run တဲ့ အခါမှာ config file ရဲ့ full path ကိုတော့ထည့်ပေးဖို့လိုပါလိမ့်မယ်။
    
    ```bash
    soketi start --config=soketi/config.json
    ```
    
5. Run ပီးပီဆိုရင်တော့ သက်ဆိုင်ရာ config ချထားတဲ့ port တွေမှာစမ်းလို့ရပါပီ။ ပေးထားတဲ့ config အတိုင်းစမ်းမယ်ဆိုရင်တော့ console၊ [http://localhost:6001](http://localhost:6001) နဲ့ [http://localhost:9601/usage](http://localhost:9601/usage) တို့မှာ အောက်ကပုံတွေအတိုင်း result ကိုမြင်ရမှာဖြစ်ပါတယ်။
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1685900428882/d3c64fbd-7fbf-4fa2-b373-7abf04b497ee.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686021808213/496c7971-7fcf-418b-8dbf-99684af93fdd.png align="center")
    
    ![http://localhost:6001](https://cdn.hashnode.com/res/hashnode/image/upload/v1685900168346/242df316-440a-410c-a659-6f6a5e639714.png align="left")
    

### Production

တစ်ကယ်လို့ soketi server ကို Production အတွက်သုံးမယ်ဆိုရင်တော့

* Multiple Apps အတွက် သုံးမယ် APP Management CRUD လိုချင်တယ်ဆိုရင် [Array Driver](https://docs.soketi.app/app-management/array-driver#app-level-limits) အစား [SQL Driver](https://docs.soketi.app/app-management/sql-drivers)၊ [Dynamo DB](https://docs.soketi.app/app-management/dynamodb) ကိုသုံးသင့်ပါတယ်။
    
* Performance အတွက် Configuration Docs ကိုသေချာဖတ်ပီးတော့ configure လုပ်ဖို့လိုပါမယ်။ [https://docs.soketi.app/advanced-usage/](https://docs.soketi.app/advanced-usage/) မှာ သေချာဖတ်လို့ရပါတယ်။Environment Variable တွေကို ကိုယ်လိုအပ်သလို သေချာ configure လုပ်နိုင်ရင် တော်တော်ကို အဆင်ပြေတာကိုတွေ့ရပါလိမ့်မယ်။
    
* Cache And Queues တွေကိုလည်း support ပေးထားပါတယ်။
    
    * [https://docs.soketi.app/advanced-usage/caching](https://docs.soketi.app/advanced-usage/caching)
        
    * [https://docs.soketi.app/advanced-usage/queues](https://docs.soketi.app/advanced-usage/queues)
        
* Process Manager တစ်ခုခုကိုသုံးဖို့လိုပါလိမ့်မယ် ကျွန်တော်က တော့ supervisor ကိုသုံးရတာသဘောကျပါတယ်။ Supervisor config ကို အောက်မှာ copy ယူလို့ရပါတယ်။
    
    ```bash
    [program:soketi]
    process_name=%(program_name)s_%(process_num)02d
    command=soketi start --config=/home/ubuntu/soketi/config.json
    autostart=true
    autorestart=true
    stopasgroup=true
    killasgroup=true
    user=ubuntu
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/var/log/soketi-supervisor.log
    stopwaitsecs=60
    stopsignal=sigint
    minfds=10240
    ```
    

### Laravel + Laravel Echo Application

တစ်ခုသတိထားဖို့က Soketi က **Pusher compatible** ထုတ်ထားတာဖြစ်ပီး Laravel Echo ကလည်း Pusher အတွက်အဆင်ပြေတာမလို့ **config ကိုပြုပြင်ရုံကလွဲပီး တစ်ခြားကုဒ်ကိုထပ်ပြင်စရာမလိုတော့ပါဘူး**။ အောက်က links တွေမှာ အသေးစိတ်ဖတ်လို့ရပါတယ်။

[https://docs.soketi.app/getting-started/backend-configuration/laravel-broadcasting](https://docs.soketi.app/getting-started/backend-configuration/laravel-broadcasting)

[https://docs.soketi.app/getting-started/client-configuration/laravel-echo](https://docs.soketi.app/getting-started/client-configuration/laravel-echo)

အဆင်သင့် စမ်းချင်တယ်ဆိုရင်တော့ Laravel Vue နဲ့ရေးထားပြီး soketi အတွက် configured လုပ်ထားတဲ့ Sample Chat Application ကို [ဒီလင့်ခ်](https://github.com/NayThuKhant/Soketi-With-Laravel-And-Vue) ကနေယူနိုင်ပါတယ်။ Configuration ပိုင်းကိုအဓိကပြချင်တာမလို့ Clean Code ဆိုတာကိုမေ့ထားပေးပီး ဖတ်ရှုနိုင်ပါတယ်။

Soketi ကို ကောင်းကောင်း configure လုပ်နိုင်ဖို့ သူ့ definition တွေကို နောက်မှ ဆောင်းပါးတစ်ခုထပ်တင်ပေးပါ့မယ်။

Nay Thu Khant

Solution Architect @ [**onenex.co**](http://onenex.co)
