-3

I recently found out about websockets, which seem to be the best alternative for letting the client (iOS app in my case) receive new data in real-time.

I'm a relatively new Swift developer, so I know absolutely nothing about websockets, Node.js or JavaScript, three things that seem to be kind of important when using Socket.IO.

It's discouraging to see there are absolutely ZERO resources online for JavaScript-noobs like myself. All the tutorials I found only demonstrate how to set up a Chat room in some sort of localhost server in Node.js.

The tutorial that came closest to what I was looking for was this Appcoda tutorial, but even that one only demonstrates how to make your client communicate with yet another localhost server.

Can someone explain to me how I can configure a web socket on an actual server (the same server I use for retrieving and storing data with PHP), or is this not possible?

Some better resources on how to set up an actual server-client connection are also welcome, instead of these vague localhost demo's.

Fabien
  • 4,862
  • 2
  • 19
  • 33
Freddy Benson
  • 735
  • 4
  • 11
  • 30
  • Read better, he is talking about Swift... – Fabien Jul 14 '17 at 22:49
  • It's a bit hard to follow what you're asking. If you want to have incoming webSocket connections on a server, you pick a server-side development environment (PHP, nodejs, Java, whatever you like) and then you find a webSocket server library that works in that environment. Then, you follow the developer instructions with that library for how to set up an incoming webSocket server. webSocket is a standard protocol. You can connect a webSocket client to ANY server-side platform that supports the standard webSocket protocol. There is nothing node.js-specific about webSockets. – jfriend00 Jul 15 '17 at 01:00
  • @jfriend00 That's what I assumed, which is exactly why I was so surprised to see EVERY single web socket tutorial have 20+ paragraphs on how to set up Node.js. What I'm asking is how to set up the web socket on the server-side. In my case, I'm used to writing server-side code in PHP (even though I'm not a native PHP programmer), so ideally I'm looking for a way to configure a web socket on my servers in PHP. I also found that on the client-side you're supposed to establish a connection with a URL (that includes a port number), so I'm pretty much figuring out how to get a working URL like that. – Freddy Benson Jul 15 '17 at 07:32
  • So, go find a webSocket library for PHP and follow its instructions. This isn't rocket science here. Here's one: http://socketo.me/. The first comment to your question 9 hours ago showed another. This is really just YOU using Google and doing some reading to find what you're looking for and then maybe ask a more specific question about where you got stuck when implementing a solution with one particular library. – jfriend00 Jul 15 '17 at 07:51
  • Also, do you not realize that node.js IS server-side. It's just a Javascript-based server environment instead of a PHP-based server environment. – jfriend00 Jul 15 '17 at 07:54
  • @jfriend00 Well, to answer your question: no, I do NOT realize that Node.js is server-side. As I've already stated in the second sentence of my OP: I have absolutely no idea what Node.js is. All I know about Node.js is that you need to have some experience with Javascript in order to use it. I think you still don't quiet get the issue I'm dealing with. Finding a websocket for PHP was never an issue. In fact, Ratchet was the first thing I found when I started searching for websocket libraries three days ago. – Freddy Benson Jul 15 '17 at 09:22
  • @jfriend00 What I’m trying to figure out is how to connect my client (my iOS app) to these libraries. I found Socket.IO, Starscream, Deepsocket, etc., and they all explain how to set up a socket on the client in a pretty simple and straight-forward way (no rocket science there). I also found a PHP websocket library that explains how to set up a websocket on the server (a little more complicated, but still no rocket science). What it doesn’t do, however, is explain how to connect to that websocket from my client (iOS app). – Freddy Benson Jul 15 '17 at 09:22
  • @jfriend00 None of these libraries (including Ratchet) explain how to set up a websocket CONNECTION (between both the server AND the client). They only explain how to configure either the server-side OR the client-side of the connection. In the particular case of Ratchet: it doesn’t mention iOS anywhere, so I’m seriously doubting if a Swift developer can even use Ratchet, unless it’s possible to connect any socket API (such as Starscream or Socket.IO) to any server-side library (in this case Ratchet). – Freddy Benson Jul 15 '17 at 09:22
  • @jfriend00 Even if that would be possible, the main question that remains (and that can’t be answered with a simple Google search query) is: what URL do I use for initializing a socket object on the client-side? Can this URL be a PHP script (i.e. `http://example.com/websocket.php`) or does it have to be a port on my domain’s server (i.e. `http://example.com:8080`)? I have a pretty strong assumption that using the `http://localhost:8000` used in all of these demo’s won’t do much good for me, but please correct me if I’m wrong. – Freddy Benson Jul 15 '17 at 09:24
  • @FreddyBenson - The URL depends upon the client and server-side libraries. If there's no port in the URL, then that means port 80 by default (there is always a port number being used). So, if your webSocket server is running on port 80, then you do not need to specify a port number (port 80 will be used by default). If your webSocket server is not running on port 80, then you have to specify the port. How exactly you specify what you're connecting to should be explained in the doc for whatever API on iOS you're using to make the webSocket connection. – jfriend00 Jul 15 '17 at 09:29
  • Read the doc for your client and server libraries and then come back here and ask a more specific question. None of what you're asking has anything to do with a webSocket standard. It's all specific to the libraries you find and choose for both client and server and how they implemented things. If you want to ask a question about a specific library and how it works and you've already studied the doc for it, then write a new question about that specific library and show what you've learned and tried and describe where you got stuck. – jfriend00 Jul 15 '17 at 09:31
  • @jfriend00 Okay, I'll get started with Ratchet and see how far I can get. I was actually just trying to figure out where to get started when setting up a web socket on the server (as a noob, assuming that Node.js was required for this because of all the tutorials online). Now I have a better understanding of how a websocket connection is constructed, so my answer somewhat got answered. Just to be clear: it IS possible to set up a web socket with Ratchet and then connect to it with my Swift app, right? I won't run into any compatibility issues halfway in, will I? – Freddy Benson Jul 15 '17 at 09:46

1 Answers1

1

First, look at a proper WebSocket client for Swift:

https://github.com/daltoniam/Starscream

https://github.com/tidwall/SwiftWebSocket

Then, look at how to properly use websockets for your needs. Nodejs is not a naked server, and there are featured solutions over websockets or events handling through websockets:

https://deepstream.io/

http://socketcluster.io/

Read the documentation and tutorials of those solutions rather than average beginner's tutorials on Internet, like this HTTP Authentification Tutorial

Also take a look at SSE events, maybe you will find them more attractive? A practical comparison here:

http://streamdata.io/blog/push-sse-vs-websockets/

Fabien
  • 4,862
  • 2
  • 19
  • 33
  • Does SwiftWebSocket require Node.js? I checked out the documentation, and in the example code provided, a `WebSocket` object is created and it's initialized with a URL that starts with `wss://`. If I was to use this API, what URL would I have to provide in order to initialize a `WebSocket` object? In all the above-mentioned Web socket clients (Socket.IO and Starscream for example), the web socket objects are initialized with a localhost URL that looks like `http://localhost:8000`, which is created with Node.js, but what does the `wss://` URL in SwiftWebSocket reference to? – Freddy Benson Jul 14 '17 at 23:54
  • Websockets is a protocol, it does not require NodeJS specifically to work on the back-end side. Still, nodejs is supported by a large library of `npm` packages. – Fabien Jul 15 '17 at 00:04