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).
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.
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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With