Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
( I am a Beginner ).
why I should use an http request - response in Node JS when i can use the Socket.IO library?
which are the advanteges of http request - response ?
Thanks to everyone.
Web-sockets(Socket.io) and HTTP both are communication protocols. HTTP uses HTTP:// or HTTPS:// and web-socket uses ws:// or wss:// to communicate with client.
Web-sockets are designed to maintain real time connection with client. But HTTP not maintaining real time connection with client. It response to the request of the client then terminate connection with client.
So if server only provide something that doesn't change in real time or the client doesn't expect to see changes in real time then having real time connection to client is a waste of server resources(server load, traffic, etc.).
Example: Think you are searching something in google. Search results appeared in google is same as after 1 hour(or more) for the same keyword. search results are not change in real time. So think if google server use socket connection instead of HTTP connection with clients. Google servers have to maintain billions of simultaneous connections with their clients for nothing.
Also read performance comparison between web-sockets and HTTP.
Http requests are the main form of communication on the web. They are used by the client (your browser) to ask something from the server.
Socket.IO is used for 2 way communications between a client and a server. (Socket.IO uses Websockets for communication, or other fallback methods if the client does not support it)
The question here is what are you building ? A website => use simple requests. An online game => Socket.IO
If you need a two way communication, then use Socket.IO, overwise requests are just fine ;)
Related
I have built a website with PHP + Apache + JS + HTML 5. Now there is a point where I have to tell the user every second if the server is connected and ready to receive data and/or internet connection is lost/available or whatever else which can tell the user to not to send data to server because of unavailability of Server or internet connection.
For this purpose I can either move to Long Polling with Ajax and keep pinging my server every second but surely this will cause alot of network overhead on my Apache Server where my clients are about tens of thousands live at a time so keep pinging the server is not a good option. Therefore, I decided to use WebSockets.
I have been googling for about 2 days but yet could not find enough good article to answer my 3 basic questions regarding WebSocket and Apache + PHP.
1) If once the WebSocket connection is made with server, then does it remain active like Long Polling with Server or what is the mechanism behind it? I mean how does WebSocket maintain its relation with server does it keep polling with server and hence there is always a connection between Client and Server?
2) If your answer to above question is yes then what network/IO overhead would be on server side if I use websockets because there is a continuous connection between Client and Server. And imagine if there are hundreds of thousands of clients online at a time what load will it create on Server Network or IO?
3) What is the Best approach to use WebSockets when using Apache + PHP on server? Any good article on this where I can study how to communicate with Apache Server using WebSockets? I found this question, but it doesn't answer well Using WebSocket on Apache server. In this question, it limits the experts to not to include any answer which has sysadmin tools, while I am asking for it if any required.
I have a VPS Server so tunning and installing some tools isn't a problem.
Any help would be highly appreciated. Thanks with Regards.
1) If once the WebSocket connection is made with server, then does it remain active like Long Polling with Server or what is the mechanism behind it? I mean how does WebSocket maintain its relation with server does it keep polling with server and hence there is always a connection between Client and Server?
Yes, it remains active and there is always a connection between Client and Server. However, the client needs to maintain the connection and on connection exceptions. In this case, it is your javascript code.
2) If your answer to above question is yes then what network/IO overhead would be on server side if I use websockets because there is a continuous connection between Client and Server. And imagine if there are hundreds of thousands of clients online at a time what load will it create on Server Network or IO?
WebSocket connections are handled on TCP/IP level and by definition, they are not resource consuming operations when there is no data going through the TCP tunnel. So rather than worrying about your CPU and memory conception, you need to worry about the limit on the number of connections. Consider using a load balancer for your socket connections and utilize multiple servers if you are expecting more than 10000 concurrent connections.
3) What is the Best approach to use WebSockets when using Apache + PHP on server? Any good article on this where I can study how to communicate with Apache Server using WebSockets? I found this question, but it doesn't answer well Using WebSocket on Apache server. In this question, it limits the experts to not to include any answer which has sysadmin tools, while I am asking for it if any required.
For such a use case of yours - getting the status of the server, I would suggest using a message broker rather than load this simple operation to Apache.
Please consider looking at mosquitto, hivemq or rabbitmq
All of them are supporting WebSockets and all of them have their pros and cons. Do the small proof of concepts over them and choose what is best for you.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm using this template and there's a sample page which covers notifications. There are numerous buttons which show a notification when pressed. I took a look at the button's source:
<a id="toastr_show" href="#" class="btn btn-dark">Show</a>
I found the javascript source code.
function runToastr(obj){
$(obj.elem).on( "click", function(e) {
e.preventDefault();
toastr.options.closeButton = false;
toastr.remove();
switch(obj.type){
...
I wonder how I can 'call' this notification from php?
There are a few ways for you.
HTML5 solutions:
Websocket
Server-sent Events
Websocket create a two-way tunnel and Server-sent Events create a one-way tunnel from server to client. Both of them can make your server side program send a "notification" to your client, so that you can display it via Javascript.
How ever, HTML5 doesn't work in any browser. And its programing is kind of difficult.
The easiest way is to make a AJAX request periodicity. However, it means the notification is not real time. And a short period will cost batteries on your client and resources on your server
To trigger notifications from a server side to a client, the following technologies may be implemented:
1. Websockets. This is a relatively new feature in HTML5. It keeps a CPU-less-intensive port to the server open and listens to that port. Now the server passes whatever it must pass to the client and the client renders it real time. Its application is in chat applications. But so far, PHP capability for that is kinda limited. NodeJS however has Socket.IO with a lot more. You might want to read about Websockets here.
2. A clever workaround is to request for notifications using an interval request. You do this by simply wrapping the request inside a JavaScript setInterval() method. If you are using jQuery, be sure to put this into the $(document).ready(function() {});
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Scenario
I am developing some sort of web based cloud storage service.
One feature is that the user can initiate transcoding of video files (so that they can be streamed on different devices).
This takes some time and I want to display a progress bar to the user.
My plan is to submit the job using ajax where it is written into a database. The ajax call returns the ID of the job in the database, and this id will be used as the channel for notifications.
So when the job has been submitted, the client subscribes to the channel "job-databaseID" on some self hosted websocket server.
Transcoding workers then periodically select pending jobs from the database table and process them. While processing they push their progress to the websocket server to the same channel where the client is listening.
The front-end application should be a website with javascript and jquery.
The back end should be programmed in PHP and MySQL and an apache or nginx webserver.
Question
Is this a proper way of using websockets?
Usually I see websockets employed in a on-to-many notification scenario. Here it is a one-to-one notification scenario.
Are there maby better alterantives for this kind one way information flow?
Also I often see channels for Websocket scenarios to be more or less long-lived. Here it is very short-lived. Would it maby make more sense to make one channel per user?
What would be a good websocket server for that kind of use? Ideally the channels would be auto-removed once no client is connected to it any more and auto-created the same way, so I don't need to take care of that.
Did you take a look at Server Sent Events as you are initiating your request via ajax so you aren't performing bidirectional communication; you only want server to push you updates when it has ones
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
A while ago I built a small app using python which allowed users to create small chat rooms. I implemented the server as nothing more than a rest API. The client makes requests and the server gives the appropriate response. The response is recieved by a JavaScript client which keeps on making subsequent requests to the server for updated data. Currently the client sends around 1 request every 3 seconds. I would like my app to be real-time(updates appearing as soon as changes are made), for that to happen I would have to narrow the request interval further to around 0.7 seconds. The problem with this approach is that it is not entirely scalable. Is there any way the server can send data to the client when updates occur?
Instead of having the clients pull the server for data, you can have the server push data to the client. The most common protocol to do so (and it is quite close to the standard HTTP protocol) is called WebSockets. WebSockets are an evolving standard, not all browsers support them equally.
Also, in case you use a reverse proxy server at the edge of your network, not all reverse proxy servers support WebSockets, which may also prevent you from using WebSockets.
See: http://en.wikipedia.org/wiki/WebSocket
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Scenario:
I have a webserver running php, I want to be able to be able to securely send a request to a separate server running nodejs and get a response back.
The node server will never need to send anything to the webserver by itself, e.g notifications/updates. So I don't think I need the 2 way communications that websockets would provide.
PHP sends a request to node, node processes the request, sends back data (most likely in JSON), php continues with it's script using the returned data.
Problem
I own the server running node, but the webserver is out in the wild. I need a way of making sure that any requests that come in to the node server are actually from the webserver not someone/something else, and I need the request and response data to be encrypted.
I have gathered I don't want to rely on something simple like checking ip addresses, I know that the webserver and the node sever will have to both have some shared secret information/algorithms to encode data. I could have a go at implementing this myself, but I know this problem is already solved with some encryption protocol / libraries.
I'm familiar with the concepts of encryption and keys, but I have never had to implement them.
Question
What is the best way to go about this?
What kind of encryption should/can I use, that is both supported by php and nodeJs?
What would be the potential security threats, if any?
I would suggest interact between two web servers using REST APIs.
REST APIs are used in these types of implementations.
If you can build proper authentication strategy, then the communication should be secure. If you enable SSL on Node, the communication will be encrypted. You can also limit by IP addresses, hostnames, and user agent strings.
Token based authentication with SSL should be good enough security. Utilize a strategy that implements nonce, and always have the tokens expire.
Implement CSRF strategy to prevent MITM attacks.
Build a token-based strategy that relies on common encryption methods, such as:
OAuth2
WSSE on Symfony2
CSRF on Express