Can cfwebsocket be used without the generated JavaScript mess?
Asked Answered
C

1

6

When CF8 brought us ajax support, <cfajaxproxy> and various <cfform> tags made ajax with CF real easy, but invoking cfc?method= is also possible with any JS lib, and one can get away with including those horrific JavaScript from Adobe.

Now that CF10 brought us websocket support, but is <cfwebsocket> the only way to use websocket with CF?

Consultation answered 9/1, 2014 at 8:56 Comment(1)
Quick Google search turned up this: github.com/nmische/cf-websocket-gateway/wiki which appears to be a way to use WebSockets in earlier versions of ColdFusion as well.Turman
G
1

Henry, There are many other ways to use websockets because CF includes a fairly robust gateway service. It has been there since CF 7.

On my CF 9 server I use the product by Nate Mische that Scott indicated in his link. I added the gateway type, then added an instance. I send events to my instance from my internal tracking system where I tracked logged hours for my many consultants. Then I built a dashboard that includes realtime charting plus hours, notes and raw SVN comment updates. It gives me a living picture of what is happening in my company during the working day. Clicking on pie sections of the charts brings up additional chart data using the socket. For example, clicking on a pie segment for a client brings up daily hours burn for that client. It is much faster (generally) that an Ajax call. I use client side charting libraries called "Rgraph" for the visuals.

It looks like this.

dashboard

Under the hood on the client side the code is what you would expect. Note these samples are not the full story - your implementation would be unique.

<script>

var reconnectTimer = 0;
var userID = '1';
var socketDomain = 'ws://*my system url*.com:1225';
// Firefox is a little different.
if (window.MozWebSocket) {
  window.WebSocket = window.MozWebSocket;
}

...
</script>

I open a connection (conn is the var) and then there is an onMessage() function that parses out each message type (messages arive as json - but they could be any sort of string).

 conn.onmessage = function (event) {

      var message = event.data;
          console.log(event.data);
      var t = JSON.parse(event.data);

      if(t.TYPE != 'Blah') 
                  do A B or C
....

It's not so neatly packaged up as cfwebsocket, and I suspect the socket server might be fragile on a different system - say one with a very heavy load like a stock trading application. But it works well for how it is tasked. I hope this helps!

-Mark

General answered 9/1, 2014 at 15:39 Comment(3)
Good to know, just that this is not what this Q is looking for. Thanks Mark!Consultation
"Is Cfwebsocket the only way to use websockets with CF" .... no it is not. See answer above. :)General
Maybe I should word it to "Is <cfwebsocket> the only way to use CF10's websocket?" I actual quite like your solution, then I can bypass the 100 connections limit in CF10 Standard Edition.Consultation

© 2022 - 2024 — McMap. All rights reserved.