With the help of ColdFusion engineer Awdhesh Kumar, I’ve discovered an important tip with ColdFusion 10 WebSockets I’d like to share. I will admit that I’m still trying to wrap my brain around the details somewhat, but this tip works well and may be useful for others.
One way I use this feature is in my demo chat applications. The subscribe API also allows for custom headers so I pass along the user’s desired username (or chatroom) name when they subscribe.
This allows me to use custom logic in the CFC handler to ensure that no two people have the same username:
Simple, right? But – it turns out that the custom data I see in the subscriber packet returned from wsGetSubscribers is not present in other CFC handlers.
As an example, I wanted to use beforePublish to check the username of the person making the broadcast. This method is passed a publisherInfo struct, which I assumed was the same struct as what I saw returned from wsGetSubscribers. This is not true. As it stands, you do not have to subscribe to a channel to broadcast!
Awdhesh helped me see the difference here, although it took a little while to sink in. Luckily, there is a very simple fix.
In your allowSubscribe handler, you can write custom data to the subscriberInfo.connectionInfo structure. As an example, I did this:
In this example I’ve both set a hard coded value and copied a value from the subscriber metadata. Obviously you can do whatever you want here. Later on in my publisher info I’ll have access to the same data.
Hope this helps!