ColdFusion API and Websockets - websocket

I am hoping someone can point me in the right direction. I have a CF2021 Server which uses a Node.js websocket server and CF pages (via javascript) as a client. Messages from user to user work as expected, so no issue there.
This CF Server also has a custom API built using CFML that handles and routes inbound SMS messages. My question is; what would be the best way to send the SMS message (by now its json) to the Node.js websocket to it can send it to the user(s).
I tried using the same javascript that the browser client uses, but it appears that the CFML API script is "browser-less", so that doesn't work, or should it?
I thought something like Apache Groovy may be the solution, but I am having difficulties with any websocket example I have found.
Any suggestions would be greatly appreciated.
thanks in advance

Flow matters.
If you want to handle an incoming message by delivering it to all currently logged in users who are subscribed to messages of the current sort: set up your message handler to deliver using lucee/adobe-coldfusion websockets. Be forewarned, Lucee takes some setup time, but once running, it is a great solution.
If you don't need immediate delivery, or you need a super simple solution: I actually have had some success with "Long Polling" you just have to remember to use "flush" early in the request, before any pause/sleep, then loop your message lookup requests for new data, with a 1-5 second delay between each loop. Once new data is found, I like to return the request to the client, close that polling request and start a new polling request using the client side. I typically won't poll for more than 60 seconds. Even if the returned json object is empty.

Related

Trouble using Websockets with Julia

I am trying to connect to an API that uses websockets. I need to do the following:
Connect to the websocket using a given URI
Send a login request
Send a request for the required data stream(s)
Store the returned streamed data in an array for immediate processing (the array will be continually updated while data is streamed)
When finished collecting data, send a logout request
I have a general understanding of websockets, but have never tried to connect to a websocket. I have read through the “documentation” for packages HTTP (which I have used before), WebSockets, and DandelionWebSockets. Each has left me scratching my head trying to understand how to implement the above tasks.
Would someone please help by showing me, line-by-line, how to set up the above tasks and also explain why each line or function is used? (Assume I have the correct URI, login, data, and logout request formats.)

Flask AJAX: send interim data to front end during a route

I've made an app using Flask, that has a backend process that is initiated by the user.
When the user initiates the process, some data is sent to Flask via jQuery AJAX, which is then processed, and the results are returned.
This process can take between a few seconds and up to around a minute, so I have a 'please wait' modal on the front end while waiting for the AJAX response from the backend.
Is there a way I can send interim data to the front end, to update the 'please wait' modal, while the backend process is doing its thing?
The backed process performs iterations until it is satisfied. So ideally I would like to be able to display to the user how many iterations it has performed.
Initially I thought that there might be something within Flask's 'flash' message feature. But it seems that this relates more to redirects in a route, rather than AJAX calls to a route.
Any suggestions would be appreciated.
Cheers,
Hugh
Yes you can do it, but not with AJAX, becuse the HTTP comunication is only client to server, so you cant update asynchronously your clinent with HTTP, so you need to use other protocol. I highly recommend to use SocketIo, this protocol allows to you to send mensagens asynchronously from server to update your front, becuse this protocol persist the user in server. With this protocol(for example) you can make a chat room, like WhatsApp. for more information see Documentation SocketIo Flask Extension

How could detect if client close the connection on rails 4.2

I am working on an API application on Rails 4.2.0. Wonder if there any way I could tell if the client closes the connection after they made API call to the server.
Tried session and cookie, seems they do not design for it.
Every HTTP requests ends up being closed when it's complete, as that's how the HTTP request-response cycle works. It's extremely rare to have connections hanging open as long-polling fell out of style once WebSocket was standardized.
After the client has made a call you can assume that request has completed and they've disconnected. There's no way of knowing if they will make additional requests or not, it's entirely up to the client.
In response to your comment on what you are actually trying to do, the only way I can think of would be via javascript using onunload. Here is a very basic example.
<body onunload="handleOnClose()">
<script>
function handleOnClose()
{
// send something to your backend
}
</script>
</body>
For more info see this SO question

setInterval() ajax call is working on local server but giving error in online server

I have a website in which there is chatroom where I use to send AJAX request to check if a person received a new message or not. If a new message is received, it gets appended to the DOM without refreshing the page (like Facebook).
I am using:-
setInterval(check_if_new_message, 1000);
i.e. one AJAX request to check message every one second.
This was working fine as it was supposed to when I was trying to run on the local server. But then I bought Starter Shared Linux Hosting on GoDaddy and then my ajax requests are not working properly. First 100-150 requests are working fine but after that, it stars giving an error like net::ERR_CONNECTION_CLOSED in the console of the browser.
setInterval(check_if_new_message, 1000);
You can see that you are using:
setInterval(check_if_new_message, 1000);
That means you are calling check_if_new_message after every 1 second. This works well in the localhost because it is on your computer. But when you try this on a live server, you will get:
net::ERR_CONNECTION_CLOSED
This is because your server can not handle so many requests. Your server may have less RAM.
This is not a good practice for a real-time chat application.
If you want to make a realtime chat application use WebSocket for that.
Useful resources for WebSocket:
What is WebSocket?
WS library

Grails: server-to-client notification

I'm building a Grails app which queries several API's across the Web. The problem is that this queries are very time consuming and it is really annoying for the user to click one button and wait so much time without nothing changes in the page.
The basic architecture of my app is, when the user clicks the button, the client side performs an Ajax request with Prototype library to the server side. The server side, then, connects to the Last.fm API and retrieve a list of events. When the server side is finished populating the list with events it sends a JSON response to the client side which allows the client side to create markers for each event on a Google map.
What I want to achieve is, instead of waiting for all the events being retrieved from the API to send the JSON response, the server side sends a JSON response as soon as it retrieve one event, allowing the client side to populate the map while other events are yet being retrieved.
Any ideas how can I implement this? I read about the concept of Ajax Push but I'm not sure if it is what I need.
Thanks for the help!
There is no way to open a listening connection on the client that your server might connect to. Instead, what you need is a connection to the server that is kept alive and can be used to receive events. This way, you could directly return the "action" request and inform the client through your persistent event connection once the last.fm request was completed.
That said, the way I would implement is using ajax keep alive.
Take a look at cometd plugin.

Resources