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

  • Everything You Need To Know About Socket.IO
  • Configuring SSO Using WSO2 Identity Server
  • What Developers Need to Know About the Price Checker App
  • Auto-Scaling a Spring Boot Native App With Nomad

Trending

  • OneStream Fast Data Extracts APIs
  • Navigating the Skies
  • Build a Digital Collectibles Portal Using Flow and Cadence (Part 1)
  • How to Migrate Vector Data from PostgreSQL to MyScale
  1. DZone
  2. Coding
  3. Tools
  4. Sharing ColdFusion WebSockets Among Different Applications

Sharing ColdFusion WebSockets Among Different Applications

Raymond Camden user avatar by
Raymond Camden
·
Aug. 11, 13 · Interview
Like (0)
Save
Tweet
Share
4.21K Views

Join the DZone community and get the full member experience.

Join For Free

This question came in today and I thought it was pretty interesting:

We are building a family of applications (using Coldfusion and Coldbox), all of which run off the same database, for a client. I've also built a chat system (using ColdFusion WebSockets) for one of the apps. So, people logged on to that app can chat with one another.

But we think it would be cool if the chat system could be extended across all the sister apps. That is, a person logged on to any one app could chat with a person logged on to another sister app. Since we define WebSocket channels at the application level, it seems channels cannot span across multiple applications (but that's what we want to achieve).

So, in case you don't quite get it, ColdFusion 10's WebSockets are specific to one application. Given a WebSocket channel named "chat" in application one, if you have one with the same name in application two they won't share messages.

One of the first things I did was recommend Node.js. And no--I wasn't saying to give up ColdFusion. You can easily use Node.js services alongside ColdFusion. If you were to set up a Node.js websocket server on, say, port 8888 then your apps could all make use of it. (I first saw Simeon Bateman demo this.)

But outside of that I suggested using a simple iframe. Dropping in an iframe would let you quickly include your chat application inside of it. I built a quick demo of this. I created two apps (and by apps I mean one page with a dump of the application scope) and then simply added an iframe to the chat app:

This is the index page for an app.

<cfdump var="#application#">

<iframe src="../chat" style="float:right;width:400px;height:400px"></iframe>

The result is pretty much what you expect. As an FYI, my chat demo wasn't really built for an iframe. It could be improved.

So ... there's that. But I was curious about doing something a bit more than chat. What if you wanted the iframe to communicate with the parent window? Turns out that's slightly complex due to browser security issues, but that may not impact you depending on your use case. (For the book on the topic, see Third-Party JavaScript.)

I began with a simple example where all apps were hosted on the same domain:

  • foo.com/app1
  • foo.com/app2
  • foo.com/chat

In that case, your chat app could communicate to the parent rather easily:

if(window.parent.msgHook) window.parent.msgHook(message.data.chat);

Where in app1 I may have:

function msgHook(m) {
  console.log("msghook");
  console.dir(m);
}

I'm not actually doing anything with the message, but you get the idea.

Moving to different domains though gets tricky. Imagine you have these domains:

  • x.foo.com
  • y.foo.com
  • chat.foo.com

If you want X and Y to be able to iframe the chat server and receive messages, you have to use something like postMessage. In compatible browsers, this lets you add event listeners for messages being broadcast from one iframe to a parent (or vice versa).

As an example, I added this bit of code in my app:

window.addEventListener("message", function(e) {
  console.log("msg listener");
	console.dir(e);
});

And then my chat server broadcasted a message using postMessage. Note that I'm using * for the targetOrigin. I can make this more specific to my domains.

window.parent.postMessage({"msg":"foo"},"*"); 

Make sense? I've included all 3 iterations of the app as an attachment to this entry.


application WebSocket app

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Everything You Need To Know About Socket.IO
  • Configuring SSO Using WSO2 Identity Server
  • What Developers Need to Know About the Price Checker App
  • Auto-Scaling a Spring Boot Native App With Nomad

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: