Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can cfwebsocket be used without the generated JavaScript mess?

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?

like image 733
Henry Avatar asked Jan 09 '14 08:01

Henry


People also ask

Does WebSocket use JavaScript?

JavaScript is important to WebSocket only in browsers because the browsers have implemented the WebSocket API (See RFC 6455) in JavaScript. So if you want to access WebSocket from within an HTML5 page, you need to write JavaScript.

How do JavaScript Websockets work?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

How do I start a WebSocket server?

To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new WebSocket("ws://javascript.info"); There's also encrypted wss:// protocol. It's like HTTPS for websockets.


1 Answers

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

like image 156
Mark A Kruger Avatar answered Sep 30 '22 20:09

Mark A Kruger