skip to Main Content

I need your help about Websocket with iOS.

I am trying to connect to websocket server using Node.js from iOS client using Socket.IO-Client-Swift, but it seems Node.js websocket server does not recognize access from iOS client.

/etc/nginx/conf.d/rsp.arakaki.app.conf

(Configuration file for nginx)

Websocket server is running on port 3000, so requests to /socket.io/ will be proxied to upstream websocket (server localhost:3000;).

upstream php-fpm {
  server localhost:9000;
}

upstream websocket {
  server localhost:3000;
}

server {
  server_name rsp.arakaki.app;

  listen 80;

  return 301 https://$host$request_uri;
}

server {
  server_name rsp.arakaki.app;

  listen 443 ssl;

  ssl_certificate /etc/letsencrypt/live/rsp.arakaki.app/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/rsp.arakaki.app/privkey.pem;

  root /var/www/rsp.arakaki.app/webroot;

  index index.php;

  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location /socket.io/ {
    proxy_pass http://websocket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
  }

  location ~ .php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass php-fpm;
    fastcgi_index index.php;
    fastcgi_intercept_errors on;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

/var/www/rsp.arakaki.app/websocket/src/server.js

(Source file for websocket server)

I suppose that websocket server logs some messages when websocket client successfully connected to the server. But I can only see the messages when I use Web client. I cannot see any message when I use iOS client.

const fs = require("fs");

const express = require("express");

const app = express();

const http = require("http");

const server = http.createServer(app);

const io = require("socket.io")(server);

io.on("connection", (socket) => {
  console.log("a user connected");

  socket.on("disconnecting", (reason) => {
    console.log("user disconnecting", { reason });
  });

  socket.on("disconnect", (reason) => {
    console.log("user disconnected", { reason });
  });
});

server.listen(3000, () => {
  console.log("server running...");
});

MainViewController.swift

(iOS websocket client)

Websocket server logs nothing with this code.
I think it is because of iOS client since I succeeded to connect from web client.
I have no idea.

import UIKit
import SocketIO

class MainViewController: UIViewController {
    var manager: SocketManager!
    var socket: SocketIOClient!

    override func viewDidLoad() {
        super.viewDidLoad()

        manager = SocketManager(socketURL: URL(string: "https://rsp.arakaki.app/")!, config: [.log(true), .forceWebsockets(true)])
        socket = manager?.defaultSocket

        socket.on(clientEvent: .connect) { (data, ack) in
            print("socket connected")
            self.socket.emit("join", ["hoge"])
        }

        socket.on(clientEvent: .error, callback: { (data, ack) in
            print("socket error")
        })

        socket.connect(timeoutAfter: 3, withHandler: {
            print("socket timeout")
        })
    }
}

websocket.php

(Web websocket client)

This script works well. Websocket server logs as connected.

<script src="https://rsp.arakaki.app/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

Environment

  • CentOS 7
  • Nginx (1.18.0)
  • Node.js (14.15.3)
    • socket.io (3.0.4)
    • express (4.17.1)
  • Swift 5
    • Socket.IO-Client-Swift (15.2.0)
  • Xcode (12.3)

Thank you for your interest. Any comments are welcome. Please help me.

2

Answers


  1. Chosen as BEST ANSWER

    Socket.IO-Client-Swift could connect to server after I downgraded socket.io library for nodejs server to version 2.0.4. Thank you.


  2. Your "Socket.IO-Client-Swift" version is 15.2, it is not compatible with socket.io server’s 3.0 version.

    "The client now supports socket.io 3 servers." is written in "Upgrading from v15 to v16" guide: https://nuclearace.github.io/Socket.IO-Client-Swift/15to16.html

    You must use v16 minimum for server 3.0 version, but already not released. Probably it is coming soon.
    So using the socket.io server’s 2 version it is good solution for as now.

    You can look version tags for ios client at the below link:

    https://github.com/socketio/socket.io-client-swift/tags

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search