Websocket

Good morning!

I’m trying to use the websocket extension, but I get the error “Firefox cannot establish a connection with the server”. Here what I did and tried :

  1. I installed Lucee WebSockets Extension 2.0.3 by Igal Sapir
  2. I added the file servlet-filter-utils-1.1.1.jar in C:\lucee\tomcat\lib
  3. I added the following code in web.xml in C:\lucee\tomcat\conf
<!-- Required for the Lucee WebSocket Extension !-->    
    <filter>
      <filter-name>HttpSessionInitializerFilter</filter-name>
      <filter-class>net.twentyonesolutions.servlet.filter.HttpSessionInitializerFilter</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>HttpSessionInitializerFilter</filter-name>
      <!-- modify url-pattern to match your websocket endpoints !-->
      <url-pattern>/ws/*</url-pattern>
    </filter-mapping>
  1. I restarted Lucee
  2. I installed the module WebSocket Protocol in IIS
  3. I created a folder test in my website with the file echoListener.cfc with the onMessage function.
  4. I created index.cfm in the folder with the following code :
<h1>WebSocket Echo Example</h1>
<cfscript>
    var test = "";
    endpoint = "/ws/echo";
    listener = new EchoListener();
    test = WebsocketRegister(endpoint, listener);
</cfscript>

<p class="mt2m">Server side code:
<p>
<code class="block"><cfset echo(htmlCodeFormat('
    <cfscript>
        endpoint = "/ws/echo";
        listener = new EchoListener();
        WebsocketRegister(endpoint, listener);
    </cfscript>
'))></code>

<cfsavecontent variable="js">
    <script type="text/javascript">
        var endpoint = "<cfset echo(endpoint)>";
        var protocol = (document.location.protocol == "https:") ? "wss://" : "ws://";
        var url = protocol + document.location.host + endpoint;

        var wsecho = new WebSocket(url);

        var log = function(evt){ console.log(evt); }
        wsecho.onopen    = log;
        wsecho.onmessage = log;
        wsecho.onerror   = log;
        wsecho.onclose   = log;
    </script>
</cfsavecontent>

<cfset echo(js)>

<p class="mt2m">Below is the JavaScript code that was used to set up the <code>wsecho</code> client:
<code class="block"><cfset echo(htmlCodeFormat(js))></code>

<p class="mt2m">Open Developer Tools and send a test message using the <code>wsecho</code>
  object, e.g.
<p>
<code class="block">
wsecho.send("Hello Lucee WebSockets at " + (new Date()).getTime());
</code>

<style>
    code   { background-color: #f0f0f0; white-space: pre-wrap; }
    .block { display: block; }
    .mt2m { margin-top: 2em; }
</style>
  1. I called the url with my browser at http://www.example.com/test/

  2. I get the error Firefox cannot establish a connection with the server. Similar error in Google Chrome. I tried with ws and wss.

Did I forgot something? What can I do to make it works?

Thank you!

OS: Windows Server 2016 (10.0) 64bit and IIS
Java Version: 1.8.0_181 (Oracle Corporation) 64bit
Tomcat Version: Apache Tomcat/8.5.33
Lucee Version: Lucee 5.3.8.159-SNAPSHOT

If you are fronting with IIS and you’re IIS is connected with boncode, then calling any WS protocol won’t work: boncode connector doesn’t support it:

http://boncode.net/connector/webdocs/Tomcat_Connector.htm#_Toc38268672

Did you try it on port 8888 to see if it’s working?

2 Likes

There is an excellent write up of how to get this working by @Redtopia on these forums:
https://lucee.daemonite.io/t/troubleshooting-lucee-websockets-with-iis-and-arr/2687/3

I have followed this to get websockets working with Lucee and IIS.

This post is also of interest as you may not require the additional jar file:
https://lucee.daemonite.io/t/installing-the-websocket-extension-on-tomcat-got-much-easier/3012

2 Likes

I just want to point out that you cannot call WebsocketRegister(endpoint, listener); more than once after your application starts up. You need to restart Lucee if you want to call it again. That code should be called in onApplicationStart() in your application.cfc file.

I do not have websockets running in IIS on development. I am using tomcat on port 8888 for development, and my websocket endpoints include the 8888 port. I do have IIS working in production and have it setup with ARR as described in the post that @martin referenced.

When websockets are not connecting, it’s possible that your websocket handler code is problematic. The way to debug is by logging everything. There’s no other way to see what’s going on other than to write log entries. If you need to dump data, use serializeJSON() to convert it to text.

1 Like

For further information on the calling of WebsocketRegister() take a look at this thread on GiHub, it mentions a couple of ways to work with this:

2 Likes

Hi,

I’m sadly overwhelmed at work now and don’t have time to test your solutions right away. Thank you very much for your time, it gives me hope that I can get this to work later. One thing is certain, when I have more time to spend on websockets, I will come back here to let you know the result, whether positive or negative.

Have a good day!

1 Like

Hi!

Better late than never. Good news, especially for me! I followed the steps in both topics provided by @martin and Websockets now work perfectly on Lucee!

Thank you!

1 Like

@TonyMonast - I am really glad that you have this up and running. We have recently put a websocket app (that uses Lucee and IIS) into production and it has been working smoothly so far.