DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • TypeScript: Useful Features
  • Tracking Bugs Made Easy With React.js Bug Tracking Tools
  • What Is useContext in React?
  • Mastering React: Common Interview Questions and Answers

Trending

  • How To Optimize Feature Sets With Genetic Algorithms
  • Harnessing the Power of In-Memory Databases: Unleashing Real-Time Data Processing
  • What Is Kubernetes RBAC and Why Do You Need It?
  • Top 7 Best Practices DevSecOps Team Must Implement in the CI/CD Process
  1. DZone
  2. Coding
  3. JavaScript
  4. ColdFusion 10 Web Socket JavaScript APIs

ColdFusion 10 Web Socket JavaScript APIs

Raymond Camden user avatar by
Raymond Camden
·
Feb. 26, 12 · Interview
Like (0)
Save
Tweet
Share
5.25K Views

Join the DZone community and get the full member experience.

Join For Free

In my last post, I demonstrated three examples of websockets under ColdFusion 10. One thing I didn't really touch on was the JavaScript API you can use to work with websockets. These functions are available to any file making use of the cfwebsocket tag. They allow you to:

  • Open or close a connection as well as checking if the connection is open (openConnection, closeConnection, isConnectionOpen)
  • Subscribe or unsubscribe to a channel (remember that the cfwebsocket tag can autosubscribe you - that's what my demos did)
  • Authenticate you - technically your back end code will do this, but this helps set up your websocket connection as an authenticated one
  • Get a list of what you're subscribed too (getSubscriptions)
  • Get a count of the people subscribed to a channel (getSubscriptionCount)
  • And finally, invokeAndPublish, which lets you use the websocket connection to run a CFC method.

Each of these functions are asynchronous. The docs clearly say this and anyone who doesn't see this may be a bit slow. (-sigh- yes... I missed it.) So as a simple example, I wanted to add a subscriber count to my chat application. I added the following code within my user registration system (the code run when you tell the application your name):

window.setInterval(function(){
    chatWS.getSubscriberCount("chat")
},2000);

Remember that "chatWS" is a JavaScript object that is my 'hook' to the websocket.

Each of the JavaScript methods will use your message handler for results. This means your message handler has to be a bit complex. Previously it handled new user arrivals as well as messages. So now I have to add a bit more logic to handle this result. Here's my message hander:

function msgHandler(message){
    //Only care about messages
    if (message.type == "data") {
        var data = JSON.parse(message.data);
        if(data.type == "chat") $("#chatlog").append(data.username + " says: " + data.chat + "\n");
        else $("#chatlog").append(data.chat + "\r");
        $('#chatlog').scrollTop($('#chatlog')[0].scrollHeight);
        console.log("Append "+data.chat);
    }
        
    if(message.type == "response" && message.reqType == "getSubscriberCount") {
        $("#userCount").text(message.subscriberCount);
    }
}

You can visit the demo here: http://raymondcamden.com/demos/2012/feb/19/chat/#

p.s. When I first tried to use this feature, I didn't realize the calls were asynchronous, even though the docs say this. I also didn't realize my message handler would get the result. I thought - why not simply get a user count after we get each message. So I put the call in the message handler. Take a while guess what this did to my server.


Source: http://www.raymondcamden.com/index.cfm/2012/2/23/ColdFusion-10-Web-Socket-JavaScript-APIs

JavaScript

Opinions expressed by DZone contributors are their own.

Related

  • TypeScript: Useful Features
  • Tracking Bugs Made Easy With React.js Bug Tracking Tools
  • What Is useContext in React?
  • Mastering React: Common Interview Questions and Answers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: