What are webhooks in the BugHerd API and how can they be created? - webhooks

I'm currently working with the BugHerd API.
Please suggest how to create webhooks. I have google understand they are new functionality in apis and they used for two way communication.

They're a way of asking a remote server to make a request of your own server when events happen on the remote server. They allow you to receive real-time events without constantly polling the server. They're basically callbacks that happen between two severs over HTTP.
As for creating them, the API is pretty clear. Make a post request to POST /api_v2/webhooks.json and include JSON in the following format:
{
"project_id":1,
"target_url":"https://app.example.com/api/bugherd_sync/project/1/task_create",
"event":"task_create"
}
You're telling it which event to hook into, and which URL their server should contact when the event occurs.

Related

How to handle the dataflow on Node and Next.js

A few questions about how to handle this dataflow. To notice I'm new to node and next.js but doing some starting basics and now trying to think ahead. Because I have read websockets and node being good for live-data handling.
We have an external API from a game. We have multiple moderators/clients that login to our system so they can use that API. Now they are requesting with multiple clients on that API multiple times per second which isn't a nice dataflow.
The main GET functions from that API are chat, so best would be live-data/push?? Else 1 second refresh for it. And the server info with the players. That could do with a 5 second refresh. Or is it just as easy to get that data with the 1 second/live-data from the chat ownGET flow I have in mind, as mentioned next? That's the first question sneaked in.
Then the ownGET flow.
I was thinking of making a own API that does the request to the game API. The game API doesn't support a websocket. So the idea is to let the own API do a request once a second to the game API. And then let the clients listen to the own API by websocket.
What I think websocket can do is, sending a push notification to the client when there is new data. On that push notification the client sends a get request to the own API. Instead of doing a 1 second get ping per client on the own API.
Just a quick check, is this even possible? And is this the right way of thinking or could it be optimized even better? Thanks in advance!

nodejs get response from api without refreshing page

EDIT:
I started nodejs few days ago and i want to understand one thing.
Imagine that i have a nodejs web communicate with API, when i send request (offer for a specific user) i would like to know if user has accepted or declined the offer without refreshing the site. I know there is a way with AJAX, but is there any better solution how can i get state of the offer if it is accepted/declined (if something change)
Every advice is appreciated!
If you want to know in the client when the state of something on the server changes (at some indefinite future time), then here are three options:
You can regularly "poll" the server every so often with an ajax call asking for any updates on the offer. The server can then return the current state of the offer and the client can update the status in the current web page.
You can create a webSocket or socket.io (socket.io is an API on top of a webSocket) connection from the client to the server. This is a long lasting connection which allows the server to send data to the client at any time. So, anytime the server sees a change in the state of that offer, it can send an update to the client and the client can then modify the current page to show that change.
You can use the newer server-sent events which is an extension to http which allows a server to send data to a client to accomplish something similar to the previous option.

Is it possible to receive webhook events in web extensions?

We need to get webhook events from a domain in the web extensions itself. That domain is not under our control.
We get the web extension's URL using browser.identity.getRedirectURL(). We have registered this as the webhook POST callback URL in other domain.
Is it possible to receive the webhook events whenever the other domain POSTs the data in callback URL? Would it be sufficient to intercept HTTP headers in order to get the data or would we need to have Node modules/servers inside the web extension?
No, not like you described. This is a pretty deep misunderstanding how webhooks and/or browser.identity work.
Your webextension is running on a client machine; it's not a webserver listening for connections (an extension can't do that at all).
So whenever some other machine that emits a webhook event tries to connect to the endpoint provided, whatever it connects to is not your extension.
You make an allusion to browser.identity.getRedirectURL() and seem to think that this is a real address that is assigned to your webextension and others can POST to it (and your extension be somehow informed about it).
This is not the case: instead, it's a "virtual" URL that the browser will treat specially if you (the browser) navigates to it. That request never actually leaves your machine to some server. No other client can connect to it (except for other browsers with the same extension - but again it will only ever reach them).
A solution for receiving webhooks would be to have an actual webserver somewhere that can receive them, plus some sort of push mechanism to inform your extension of the event:
A persistent WebSocket connection to your "receiver" server.
GCM push messaging initiated by your "receiver" server. Not for Firefox

Creating a socket to a website to retrieve that pages information

How would I create a socket to a site to retrieve that pages information? If I'm using meteor do I have to do an HTTP.get in a loop(it's a multiplayer game so things are constantly changing) or is there a way to actually create a socket to the site. I looked at Socket.io but I was only able to find how to listen and accept, not actually create a socket to a site.
EDIT: In my case http://play.pokemonshowdown.com/ . I'm assuming if I keep doing http.get requests, it'll won't save the state and thus keep creating a new game. Could I somehow create a socket between my site/server to their site/server till I disconnect? Basically, whatever a browser does but I don't want to display that information but rather redirect it to another socket(not part of the question).
The answer here depends upon what the site you want to connect to supports. If it is just a web server that creates web pages, then your only option is to use an HTTP get operation to request a web page.
If the site supports a specific API, you can make an HTTP get operation to a specific API request and get a more structured response than just a web page.
If the site supports webSocket or socket.io connections with specific request/response messages defined, then you can connect with either a webSocket or socket.io.
But, it all depends upon what the receiving site supports.
If you are trying to do repeated requests from node.js, you would likely not use a loop, but more like a timer such as:
setInterval(function() {
request.get(..., function(err, response, body) {
// process error or response here
});
}, 10* 1000);
It may also be the case that the request module is of use to you since it adds a lot of functionality on top of the http module when trying to retrieve requests from an HTTP server.

Using node.js and socket.io with PHP application

I have working PHP application. It allows user create private projects and invite others in it. Now, using node.js and socket.io, I want to make real-time comments, posts etc.
What is the best architecture?
I see two solutions now.
The first is:
User sends AJAX query to PHP backend:
http://example.com/comment_add.php?text=...
comment_add.php adds
comment to database and via AMPQ (or something better?) notifies
node.js server which broadcasts comment to channel's subscribers.
The second is:
User sends AJAX query to node.js server: http://example.com:3000/comment_add
Node.js sends request to PHP backend (But how? And what about authorization?), receives response, and then broadcasts to channel's subscribers.
What is the best way? Is there another methods? How to implement this properly?
When you decided to use node.js + socket.io to make a real-time web-app, you don't need to think about PHP anymore and forget Ajax also... Socket.io will be the communication between client and server.
But yes, you can use Ajax and PHP for building websites fast, and some other functions that don't need real-time
The second way is the best method. You can use http to communicate with PHP from node.js. Authorization can be done in node.js but passing auth credentials everytime to PHP
Finally my working solution is #1.
When user establishing connection to node.js/socket.io he just send 'subscribe' message to node.js with his PHP session id. Node.js checks authorization using POST request to PHP backend and if all is OK allows user to establish connection.
Frontend sends all requests to PHP as it was before node.js.
PHP modifies some object, checks who can access modified object and sends message (via AMQP or redis pub/sub etc.) to node.js:
{
id_object: 125342,
users: [5, 23, 9882]
}
node.js then check who from listed users have active sockets and for each user sends GET request to PHP:
{
userId: 5,
id_object: 125342
}
Special PHP controller receiving this request runs query to get object with rights of given user id and then sends message to node.js with resulting answer. Node.js then via socket sends answer to user's frontend.
I faced this same question a year ago when starting my final year project at University. I realized that my project was much better suited to using Node as a standalone. Node is very good at dealing with I/O, this can be anything from a HTTP requests to a database query. Adding in a PHP based Web Server behind Node is going to add un-needed complexity. If your application needs to perform CPU intensive tasks you can quite easilly spawn 'child' node processed which perform the needed operation, and return the result to your parent node.
However out of the two you methods you have mentioned I would choose #2. Node.js can communicate with your PHP server in a number of ways, you could look at creating a unix socket connection between your PHP server and Node. If that is unavailable you could simply communicate between Node and your PHP back end using HTTP. :)
Take a look here, here is a solution to a question very similar to your own:
http://forum.kohanaframework.org/discussion/comment/57607

Resources