Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion 10 - Live One on One chat with Websockets

Does anyone know of any examples or pages that I can go to that implements a Live one on one chat using the CF10 Websockets? All the examples I found on the net were those of group chats where users subscribes to a certain channel. I need it so that there can be many instances of a one on one chat like how a Live Help Chat works that you see quite often on websites that allow you to chat with one of the support agents. Any help is appreciated and hopefully there will be examples (CF and JS).

like image 339
Guest Avatar asked May 24 '13 20:05

Guest


3 Answers

Ben Nadel has a nice article about using CF10's websockets for pushing a message to a target user. He even added a nice demo video. This might be what you are looking for or could at least help you get started.

like image 147
jan Avatar answered Sep 30 '22 23:09

jan


Here is some sample code that is currently working for me.

Instead of using the subscribeTo attribute, use the js function to subscribe the user and pass in some header values. These headers can then be used as filters on the publish call using selector

Example:

<cfwebsocket name="ChatSocket" onOpen="openHandler" onMessage="msgHandler" onError="errHandler">

<script>
    function openHandler(){
        //Subscribe to the channel, pass in headers for filtering later
        ChatSocket.subscribe('chatChannel',{name: '#Session.Auth.FirstName#', UserID: '#Session.Auth.UserID#', AccountID: '#Session.Auth.AccountID#' });
    }

    function publish(txt, userID){
        var msg = {
            AccountID: "#Session.Auth.AccountID#",
            publisher: '#Session.Auth.UserID#', 
            id: userID,
            message: converthtml(txt)
        };
        //When including headers, the "selector" is where you will filter who it goes to.
        var headers = {
            AccountID: "#Session.Auth.AccountID#",
            publisher: '#Session.Auth.UserID#', 
            id: userID,
            selector: "UserID eq '"+userID+"' and AccountID eq '#Session.Auth.AccountID#'"
        };
        ChatSocket.publish('chatChannel',msg, headers);

    }

    function msgHandler(message){
        console.log(message);
    }

    function errHandler(err){
        console.log(err);
    }
</script>
like image 29
Sean B. Avatar answered Sep 30 '22 22:09

Sean B.


At first I was thinking about implementing something similar but there are some rudimentary limitations in CF10 as of now that detours me from investigating further.

  1. WSS support is missing, see: Does CF10 support secure websocket wss?
  2. Websocket doesn't work in a cluster environment, see: https://groups.google.com/forum/#!topic/houcfug/M7YQQyrBTaQ

I would look elsewhere for any serious one-to-one live chat solution, maybe Socket.IO on NodeJS or Java

WSS may be coming in CF11. I'm not sure.

like image 40
Henry Avatar answered Oct 01 '22 00:10

Henry