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.
Related
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.
How I can use app.use in socket.io? E.g.
io.on('connection', function(data){
app.use('uri here', function(req, res){
// emitting here
});
});
It's really?
I have:
ss1.example.com (head-server for caching online users with users servers).
ss2.example.com (first app server)
ss3.example.com (second app server)
You're misunderstanding something here. The sequence of events when a web page is loaded is as follows:
User initiates page load (by clicking on something or by typing something in the URL bar or by selecting a bookmark).
Browser parses the server, gets the host and port out of the URL and sends an http GET request for the path to the IP address for that host and port.
Web server receives the GET request and sends back to the browser an HTML page.
The web server may or may not use middleware when that request is received (depending upon what it wants to do).
Browser parses the HTML page and then runs scripts in the page.
Javascript in the web page runs and initiates socket.io connection to some host (often to the same host that the web page came from).
Web server receives socket.io request and socket.io handle recognizes the web request as a socket.io connection request.
Server-side socket.io code responds to client request for socket.io connection and a socket.io connection is initiated between browser and server.
Client or server can then send data over the socket.io connection.
Now, to your question about where to insert app.use(). That is for http middleware. You would insert that in the regular web server request chain, typically right before you app.get() and app.post() request handlers. You would not typically use http middleware for a socket.io connection.
If you wish to run some code before any socket.io connection connects, then you would use io.use() and use a socket.io middleware handler. That will give you access to the http request information on every socket.io connection request.
If you wish to run some code on every socket.io message that is received (regardless of the message name), that is not a supported feature of socket.io. There are some add-ons that hack into socket.io to allow you to essentially do a socket.on('*', ...) type event handler for all incoming socket.io messages, but that is not something that socket.io supports as a built-in feature.
If one of these options still doesn't sound like what you want, then please explain to use what actual problem you're trying to solve and we can better make a suggestion for that particular problem.
Note your question is a bit like an XY problem where you asked how to do what you think is the solution (using app.use() for socket.io) rather than describing the actual problem you want to solve. The problem with that type of question is that if you're wrong about the solution direction, then all we can really tell you is that you're wrong with that solution because you didn't describe the actual problem so we can direct you to the right type of solution. In the future, you will probably get better answers if you make sure to describe the problem you're trying to solve, not just the solution you're trying.
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
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.
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