title: Daphne server cannot connect with websockets on HTTPS
body: <p>I'm deploying a Django project on the Openshift cloud. This project uses <a href="http://channels.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">channels</a> and Websockets to make it work asynchronously. The problem is that I can't successfully connect websockets from the browser to the Daphne Server I got running on the server side.</p>
  
  <p>I'm using the django (python2.7) and <a href="https://github.com/smarterclayton/openshift-redis-cart" rel="nofollow noreferrer">redis</a> cartridges to make it run.</p>
  
  <p>The post_deploy script I'm using looks like this:</p>
  
  <pre><code>...
  python manage.py runworker -v2 &amp;&amp; daphne myapp.asgi:channel_layer -p 8443 -b $OPENSHIFT_REDIS_HOST -v2
  ...
  </code></pre>
  
  <p>Here is my Django configuration. In <em>settings.py</em> :</p>
  
  <pre><code>...
  ALLOWED_HOSTS = [
      socket.gethostname(), 
      os.environ.get('OPENSHIFT_APP_DNS'),
  ]
  
  CHANNEL_LAYERS = {
      "default": {
          "BACKEND": "asgi_redis.RedisChannelLayer",
          "CONFIG": {
              "hosts": [("redis://:{}@{}:{}/0").format(
                  OPENSHIFT_REDIS_PASSWORD,
                  OPENSHIFT_REDIS_HOST,
                  OPENSHIFT_REDIS_PORT
                  )],
          },
          "ROUTING": "myapp.routing.channel_routing",
      },
  }
  ...
  </code></pre>
  
  <p>In <em>routing.py</em>:</p>
  
  <pre><code>...
  ws_routing = [
      routing.route("websocket.connect", ws_connect),
      routing.route("websocket.receive", ws_receive),
      routing.route("websocket.disconnect", ws_disconnect),
  ]
  
  channel_routing = [
      include(ws_routing, path=r"^/sync"),
  ]
  ...
  </code></pre>
  
  <p>In <em>consumers.py</em>;</p>
  
  <pre><code>def ws_connect(message):
      Group("notifications").add(message.reply_channel)
  
  def ws_disconnect(message):
      Group("notifications").discard(message.reply_channel)
  
  def ws_receive(message):
      print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
  </code></pre>
  
  <p>In the client side, I'm running this code:</p>
  
  <pre><code>var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
  var path = ws_scheme+'://'+window.location.host + ':8443/sync';
  var ws = new WebSocket(path);
  ws.onmessage = function(message) {
      console.log(message.data);
  }
  ws.onopen = function() {
      this.send('WS Connecting to receive updates!');
  }
  </code></pre>
  
  <p>Notice that I'm using port 8443 in Daphne settings and WebSockets settings because of <a href="https://developers.openshift.com/managing-your-applications/port-binding-routing.html" rel="nofollow noreferrer">this</a> documentation. Also, Daphne is bound to OPENSHIFT_HOST address because is not possible to bind it to 0.0.0.0 in Openshift (permission problem)</p>
  
  <p>The output looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/Iu8qt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Iu8qt.png" alt="first"></a></p>
  
  <p><a href="https://i.stack.imgur.com/JqIBN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JqIBN.png" alt="second"></a></p>
  
  <p><a href="https://i.stack.imgur.com/tIppn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tIppn.png" alt="third"></a></p>
  
  <p>Everything looks ok in the client side but if you remember, in <em>consumers.py</em> I had this:</p>
  
  <pre><code>def ws_receive(message):
      print "Receiving: '%s' from %s" % (message.content['text'], message.content['reply_channel'])
  </code></pre>
  
  <p>So in my terminal, the server should be printing out something like: "Receiving:  from " but it's not. What I'm missing here? </p>
  
  <p>tl;dr: Client-side websocket looks like it's connected correctly but the server is not printing out a message to confirm it.</p>