I have an API that continuously sends data to a webhook in my express server. The route in my server looks something like this:
app.post("/webhook", (req, res) => {
console.log(req.body);
res.status(200).end();
});
How do I send data to my frontend as they're coming in? Is it possible to place a websocket between my frontend and backend? If so, can anyone please point to a tutorial? Much appreciated, thanks.
I would recommend to use socket.io to enable a bidirectional comunication between your frontend and backend.
Each time the third party API sends data throught your webhook, you could trigger a socket event and receive a realtime update in your front end.
check: https://socket.io
It's simple and have a good documentation.
Related
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.
I have a http route /checkout which initiates a workflow process in Zeebe. The checkout route will return 200 response straight-away to calling client. Now, workflow will run for a while. So to push the response back to client after completion, I have a /sse separate route for server-sent events. Here, I will store all the client connection in a global map.
My doubt is how do I find the exact client to send the response back through sse once?
Example: Client A listening to /sse and calls /checkout endpoint which will return 200. The /sse has to return the response to client A after completion.
Currently, I thought of using cookie to identify the client. Is there a better way?
If you are already using cookies in your app than that's the way to go since the very purpose of cookies is identifying the client, so if you already have that, you should use it.
But if you rely on another authentication mechanism (like JWT), what you could do is using the url as a query.
So in the client instead of
let eventSource = new EventSource("/sse");
Do
let eventSource = new EventSource("/sse?authorization=jwt_token");
In the backend, you would validate that token, extract the Client ID and hit that global map with it to retrieve the corresponding connection.
(PS: instead of a global map, you should use a proper store, like redis, or an embedded key/value store like bbolt)
Is it possible to send data gathered from a Slack modal to an external site?
I’m using Slack Bolt for JavaScript
I’ve tried receiver.router.post('siteToSendData', (req, res) => { // You're working with an express req and res now. console.log('post to slack') res.send(dataToSend); });
You cannot send the captured data directly to a third party site. What you can do instead, is process the submission event.
https://api.slack.com/surfaces/modals/using#handling_submissions
Slack Bolt Reference : https://slack.dev/bolt-js/concepts#view_submissions
The captured data will be sent to your application first. You can then re-route it to the desired external website.
Here is example of payload that you can expect: https://api.slack.com/reference/interaction-payloads/views
I have some queries I am making that cause timeouts. Because the computing of the data takes too long on the server in some very specific edge cases. What is the best solution for my specific stack? I was thinking of polling the server every 20 seconds or so to see if my data is ready. But I'm not sure how to accomplish this. Or how to keep track of which client made which request that I'm currently processing.
My stack:
Graphene
Django
Apollo
Since you are using Django you can easly use Django Channel to enable websockets in Django.
With websocket you can send an asynchronous notification from the server to the client made the request when the data is ready so you don't need to poll every second.
In that link there are all the info to create a bidirectional communication. The tutorial is for a chat system, but you can easly adapt to your needs
You have options.
One is Apollo retry link which which you can use as follows
const myLink = new RetryLink({
delay: {
initial: 300,
max: Infinity,
jitter: true
},
attempts: {
max: 5,
retryIf: (error, _operation) => !!error
}
});
I think this is the least hustle way to do it.
Retry Link Doc
Second option is to use subscriptions instead of queries. Subscriptions are an open link between the client and server usually use for chat apps or any anything needed in real-time, so you can use that and the client will get a response when the server is done with calculation.
You'll have to do a bit work on the client side and server side to get it to work.
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