Search code examples
iosswiftwebsocketsocketrocketstarscream

Websockets only PONG responses are received


I have implemented SocketRocket for websocket communication in my app but as this library is too old and archived for now, we decided to implement StarScream which seems to be trustable.

However, after my migration from SocketRocket => StarScream, I found that only PONG responses are received for my websockets and other requests are not being responded.

Would anyone help me here find the issue or correct me?

Here is my SocketRocket implementation where my websockets are working fine even in current live production app.

Here is my StarScream implementation which is not working even with Production configuration, I use same production configs for both apps but SocketRocket works while StarScream fails.

If you check code, you will see lot of similarity with both implementation, even though I am not receiving proper response for StarScream implementation. I try to send ping requests every 2 seconds for which I get PONG response but I also do send auth request once socket is connected for which I never get any response while in SocketRocket I do get response for auth request.

  • connectSocket() is the function where sockets are being created. Please ignore connectQuickStashSocket() for now as I have commented it temporarily.

  • sendPing() is also omitted for now as I have to check response for auth, so, commented sendPing() call for every 2 seconds.

  • send() is the main function where writing data on socket takes place and I also get completion callback once data is written, still I don't get any response.

NOTE: Commenting/uncommenting sendPing(), connectQuickStashSocket() will not change the response and the code behaves same.

Here I am attaching logs for SocketRocket where you will see all request/response working fine:

>>>> Connected to Socket
Optional("d2ee02da-e4de-4593-90e6-36598c00982d")
>>>> 
{
  "kind" : "auth",
  "uuid" : "",
  "auth" : {
    "version" : "1.4.0",
    "privateKey" : "d2ee02da-e4de-4593-90e6-36598c00982d",
    "device" : {
      "type" : "ios",
      "properties" : [

      ],
      "id" : "A5F24445-C688-43B0-98C1-96C636EFA08F"
    }
  }
}
>>>> Finished receiving socket data in 0.0020380020141601562 seconds
>>>> Deserialized pong data of length 25 in 0.0008620023727416992 seconds
>>>> Finished receiving socket data in 0.33321893215179443 seconds
>>>> Deserialized auth data of length 95 in 7.49826431274414e-05 seconds
>>>> 
{
  "sync" : {
    "collections" : {
      "24191c17-463c-4de9-b718-cad41976460c:stash" : {
        "cache" : {
          "e4d4804e-07a4-49f5-91f4-1b65935d4d8e" : 1,
          "8834da09-a26d-4a0f-806a-f3c54fff175e" : 1,
          "98a01deb-cfc7-4939-a6ce-d2dc64afe3a8" : 1,
          "80986d67-e0a4-4f4d-85c0-e263989f0044" : 1,
          "dd54420d-553b-4f43-808e-6cbdfcd0fe76" : 1,
          "1da73786-da7c-4b3f-b99f-58b907d15cc2" : 2,
          "c32c0a9a-c754-412e-af42-b8859a56ca72" : 1
        }
      },
      "24191c17-463c-4de9-b718-cad41976460c:inbox" : {
        "cache" : {

        }
      }
    }
  },
  "kind" : "sync"
}
>>>> Finished receiving socket data in 0.421644926071167 seconds
>>>> Deserialized sync data of length 2473 in 0.000841975212097168 seconds
Processed json and written to realm in 0.0073059797286987305 seconds
Initial state processed

Here is logs for StarScream where only PONG is received and response for auth request is not received, it only shows data written as request:

>>>> websocket is connected: ["Upgrade": "websocket", "Access-Control-Allow-Credentials": "true", "Date": "Tue, 12 May 2020 16:55:18 GMT", "Expect-CT": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "cf-request-id": "02ab68f8400000dcd233395200000001", "Access-Control-Allow-Origin": "wss://mercury.speare.io", "CF-Cache-Status": "DYNAMIC", "CF-RAY": "5925aaa06d91dcd2-SIN", "Sec-WebSocket-Accept": "szN0nnvUplvghVle+n4FXOFp2cw=", "Server": "cloudflare", "Connection": "upgrade"]
Connected to Socket
Optional("d2ee02da-e4de-4593-90e6-36598c00982d")
{
  "kind" : "auth",
  "auth" : {
    "privateKey" : "d2ee02da-e4de-4593-90e6-36598c00982d",
    "version" : "1.4.0",
    "device" : {
      "id" : "A5F24445-C688-43B0-98C1-96C636EFA08F",
      "type" : "ios",
      "properties" : [

      ]
    }
  },
  "uuid" : ""
}
>>>> Received data: 47
Finished receiving socket data in 0.0007439851760864258 seconds
Deserialized data pong of length 25 in 0.0006880760192871094 seconds
2020-05-12 22:25:18.520913+0530 Stash[3952:884630] Socket done writing data => auth

Please suggest me here what I can do and how to track what is the actual problem.


Solution

  • I was facing different error which I got to know once I setup dev environment on my local machine and connected websockets over there.

    I checked logs from server and got that it was giving me zlib - gzip : invalid header.

    So, the issue was that server not sending any response as it was not getting the request in proper format.

    I updated gzip library via POD and now my sockets are working perfect.

    Whew..!