Is it possible to receive webhook events in web extensions? - webhooks

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

Related

Sending a notification to user using Node.js

I am looking for a solution to my problem. I have Node.js server serving my web application where user can log in. I want to handle a situation where one user A performs specific action and user B associated with this action gets real life notification. Is there a module that would help me or there is some other solution?
What you are describing is "server push" where the server proactively notifies a user on their site of some activity or event. In the web browser world these days, there are basically two underlying technology options:
webSocket (or some use socket.io, a more feature rich library built on top of webSocket)
server sent events (SSE).
For webSocket or socket.io, the basic idea is that the web page connects back to the server with a webSocket or socket.io connection. That connection stays live (unlike a typical http connection that would connect, send a request, receive a response, then close the connection). So, with that live connection, the server is free to send the client (which is the web page in a user's browser), notifications at any time. The Javascript in the web page then listens for incoming data on the connection and, based on what data it receives, then uses Javascript to update the currently displayed web page to show something to the user.
For server sent events, you open an event source on the client-side and that also creates a lasting connection to the server, but this connection is one-way only (the server can send events to the client) and it's completely built on HTTP. This is a newer technology than webSocket, but is more limited in purpose.
In both of these cases, the server has to keep track of which connection belongs to which user so when something interesting happens on the server, it can know which connection to notify of the event.
Another solution occasionally used is client-side polling. In this case, the web page just regularly sends an ajax call to the server asking if there are any new events. Anything new yet? Anything new yet? Anything new yet? While this is conceptually a bit simpler, it's typically far less efficient unless the polling intervals are spaced far apart, say 10 or 15 minutes which limits the timeliness of any notifications. This is because most polling requests (particularly when done rapidly) return no data and are just wasted cycles on your server.
If you want to notify userB, when both of you are simultaneously online during the action, then use websockets to pass message to a two-way channel to notify userB.
If you want to notify them whenever, regardless of online status, use a message queue.

Is there a way to intercept browser calls in node?

My app was hosted in xxx.com, which gets data from yyy.com. All API requests were triggered from client side.
Is there a way to intercept its request or response in node?
No, and Yes.
For the requests made by your client, you must have some control of the data sent back to the client in order to intercept it.
Assume a scenario where:
Client -----(request)----->Third Party App Server -------(response)-----> Client
In this case, as the back-end server never had a chance to come into picture, there is no way the server can change the data. Well of course, that is when the server doesn't come into picture.
Instead, if you send the request to the node server itself, which forwards the request to the Third Party App server, you obviously have control of the response receive and thus, you can manipulate both request and response or maybe just log it (whatever is your use case).
Client -----(request)----->NODE_SERVER---->Third Party App Server -------(response)-----> Node_Server ----> Client
What a few developers do to intercept the requests made from the client is that they write some client-side JavaScript code and embed it into the browser (Some sort of authentication).
While this works okay in case of normal requests, a person with malicious intents might just disable your front-end interception code and directly receive a response from the Third Party application.
Thus, if you really need to have access to the requests and response,
YOU MUST FORWARD THE REQUESTS TO AN APP SERVER YOU HAVE CONTROL TO.
P.S. It is not just about nodejs.

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.

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

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.

server send push notification to client browser without using polling

I am developing a web app in which I want that when some changes in database occurs, server sends response to particular client(like push notification). And I want this notification to be sent client's browser. I don't want to use polling.
What can I do?
I think it's possible using SSE, but I am not clear.
I want to know
Is it possible to send response to particular client without client's request(without polling).
How server will detect that particular client?
please help me.
There is Web Notification. And there you can see the browsers that support it.

Resources