Deployment - ship it!

You've built your application and are ready to deploy it live! This tutorial will give you recommendations on what you should do to setup your production environment to run a WebSocket server. None of these are required, but recommended. Please note this page serves as an introduction and boilerplate setup for each technology; each topic has many more detailed resources available.

Open Connection Limit

A Unix philosophy is "everything is a file". This means each user connecting to your WebSocket application is represented as a file somewhere. A security feature of every Unix based OS is to limit the number of file descriptors one running application may have open at a time. On many systems this default is 1,024. This would mean if you had 1,024 users currently connected to your WebSocket server anyone else attempting to connect would fail to do so.

Most OS's have many layers of security to prevent applications from opening too many file descriptors. Here is a great resource on the various ways to increase open file limits.

In addition to operating system restrictions the default EventLoop (stream_select) has a hard limit of 1024. You will need to install a new I/O handler as described in the next section.

Evented I/O extensions

The libev and libevent projects implement high-performance asynchronous event driven C libraries. The PHP extensions ev and event are available to interface with these libraries. They allow an application to transparently use the best kernel level evented I/O method (select, poll, epoll, kqueue, or event ports) available for the operating system it is running on.

The ev PHP extension bundles the libev C library in its source and requires no prior setup. If you want to use the event PHP extension, you need to first install the libevent library along with its headers for your operating system. For example on Debian/Ubuntu:

$ sudo apt-get install libevent libevent-dev

You may then install the ev or the event extension, either through your preferred package manager, or directly using pecl:

$ sudo pecl install ev
$ sudo pecl install event

No further setup is necessary; if either of these extensions is present, the evented I/O loop toolkit used by Ratchet will automatically utilize them, which will drastically improve concurrency.

XDebug

Disable the XDebug extension. Make sure it is commented out of your php.ini file. XDebug is fantastic for development, but destroys performance in production.

Server Configuration

There's a few different options when running your WebSocket server along side your website:

  • Run your web site and WebSocket server on the same machine using port 8080 for WebSockets and take the chance client proxies won't block traffic
  • Run your WebSocket server on its own server on port 80 under a subdomain (sock.example.com)
  • Put a reverse proxy (Nginx, HAProxy, Varnish) in front of your web server and WebSocket server

The first two options are fairly easy with the second being a decent option if you can afford a second server. The third is probably the most standard way to handle this. Most web servers are able to route WebSocket traffic to a running server while still rendering traditional PHP websites. We felt this approach is now common enough that it didn't need to be specifically documented here. There's now plenty of resources available through search. Here's a few links to documentation for some of the major proxies:

Supervisor

When running Ratchet in production it's highly recommended launching it from supervisord. Supervisor is a daemon that launches other processes and ensures they stay running. If for any reason your long running Ratchet application halted the supervisor daemon would ensure it starts back up immediately. Supervisor can be installed with any of the following tools: pip, easy_install, apt-get, yum. Once supervisor is installed you generate a template file with the following command:

$ echo_supervisord_conf > supervisor.conf

The following is a modification from the generated supervisor.conf file to run a Ratchet application from the "Hello World" tutorial:

[unix_http_server]
file = /tmp/supervisor.sock

[supervisord]
logfile          = ./logs/supervisord.log
logfile_maxbytes = 50MB
logfile_backups  = 10
loglevel         = info
pidfile          = /tmp/supervisord.pid
nodaemon         = false
minfds           = 1024
minprocs         = 200

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl = unix:///tmp/supervisor.sock

[program:ratchet]
command                 = bash -c "ulimit -n 10000; exec /usr/bin/php ./bin/tutorial-terminal-chat.php"
process_name            = Ratchet
numprocs                = 1
autostart               = true
autorestart             = true
user                    = root
stdout_logfile          = ./logs/info.log
stdout_logfile_maxbytes = 1MB
stderr_logfile          = ./logs/error.log
stderr_logfile_maxbytes = 1MB

If you're only going to user supervisor to run your WebSocket application you can now run it with the command:

$ sudo supervisord -c supervisor.conf

Links