Monday, September 7, 2020

Connect to Web Socket From Your Android App using OkHttp


In this blog I am going to explain how you can connect with Web Socket from your Android App using OkHttp. This will be useful when you want to send some data real time from your Android app to Web app. 

First add following line in your build.gradle file dependencies. 

implementation 'com.squareup.okhttp3:okhttp:3.11.0'

Now let the gradle build and sync. Next we will make a class which extends WebSocketListener from OkHttp3 and a new class and name it and add following code to it. 

import okhttp3.Response;

import okhttp3.WebSocket;

import okhttp3.WebSocketListener;

import okio.ByteString;

public final class MyWebSocketListener extends WebSocketListener {

    private static final int NORMAL_CLOSURE_STATUS = 5000;

    public MyWebSocketListener() {  



    public void onOpen(WebSocket webSocket, Response response) {

        webSocket.close(NORMAL_CLOSURE_STATUS, "Exit");



    public void onMessage(WebSocket webSocket, String text) {

        //String Meesage received



    public void onMessage(WebSocket webSocket, ByteString bytes) {

        //Meesage received in form of binary data



    public void onClosing(WebSocket webSocket, int code, String reason) {

        webSocket.close(NORMAL_CLOSURE_STATUS, null);

        //Closing socket



    public void onFailure(WebSocket webSocket, Throwable t, Response response) {




Now we have listener created. Let's create client and attach it to Web Socket and Listener.

val listener = MyWebSocketListener()

val webSocket =   OkHttpClient.Builder().build().newWebSocket(request, listener)

That's it, now you can use this instance to send or receive message.

Saturday, September 5, 2020

Create Secure Web Socket (WSS) with Ratchet PHP


Recently I was working on PHP project where we I was create secure web socket which is accessible with wss:// protocol. For this I struggled for couple of hours so here in blog I am going to explain how to do that so it can save your time. 

Earlier version was not supporting WSS but later it introduced React socket server which allows SSL connection. So here are steps you need to follow. 

First add all the required classed in your php file.

use Ratchet\Server\IoServer;

use Ratchet\Http\HttpServer;

use Ratchet\WebSocket\WsServer;

use MyApp\Socket;

In above example Socket is my class file which has all listeners. After this add auto load file.

require dirname( __FILE__ ) . '/vendor/autoload.php';

Next we will create our socket app. 

$app = new \Ratchet\Http\HttpServer(
    new \Ratchet\WebSocket\WsServer(
        new \MyApp\Socket()

Now next step is to create React Secure server.

$loop = \React\EventLoop\Factory::create();
$webSock = new \React\Socket\Server('', $loop);

We are using  so this socket can be connected from anywhere. 

Now lets create secure server by adding path to certificate and key

$webSock = new \React\Socket\SecureServer($webSock, $loop, [
    'local_cert' => 'CRT_PATH', 
    'local_pk'=> 'KEY_PATH', 
    'allow_self_signed' => true, 
    'verify_peer' => false

Now finally we will run our server.

$webSock = new \Ratchet\Server\IoServer($app, $webSock, $loop);

That's it. It will start your server to which you can connect with WSS protocol. To test you can use WebSocket ECHO test.

Hope this helps you.

Thursday, August 20, 2020

AWS PHP SDK - Create an Entry in Route53 - Laravel


In this blog I will explain you how to you can create an entry in Route53 hosted zone using AWS PHP SDK. Solution I mentioned here is particularly for Laravel app, but you can still use it in any of the PHP project. 

This solution is pretty much useful when you want to make a dynamic entry in your hosted. For example a dynamic sub domain creation. 

First of all your need install couple of packages using composer. Following are the packages. 

"aws/aws-sdk-php": "^3.148",

"aws/aws-sdk-php-laravel": "~3.0",

You can either install it by adding it in composer.json file or you can install it using composer require. 

Once the installation is done, follow the steps mentioned in below URL.

Once installation is done. You need to add following keys in your env file.



AWS_REGION (default = us-east-1)

Set the values in above keys from your AWS account. That's it your AWS SDK is ready. Now add following line your controller where you want to have logic for adding value in Route53

use Aws\Route53\Route53Client;

use Aws\Common\Credentials\Credentials;

Now next step is to create a client

$client = Route53Client::factory(array(

            'credentials' => config('aws.credentials'),

            'region' => config('aws.region'),

            'version' => config('aws.version'),


After client is created we will use changeResourceRecordSets to create an entry.

$result = $client->changeResourceRecordSets(array(

            // HostedZoneId is required

            'HostedZoneId' => 'YOUR_HOSTED_ZONE_ID',

            // ChangeBatch is required

            'ChangeBatch' => array(

                'Comment' => 'string',

                // Changes is required

                'Changes' => array(


                        // Action is required

                        'Action' => 'CREATE',

                        // ResourceRecordSet is required

                        'ResourceRecordSet' => array(

                            // Name is required

                            'Name' => 'YOUR_VALUE',

                            // Type is required

                            'Type' => 'CNAME', //A, CANME

                            'TTL' => 600,

                            'ResourceRecords' => array(


                                    // Value is required

                                    'Value' => 'YOUR_VALUE', //IP address or load balancer.








That's it and it will create an entry in Route53 hosted zone.