show notification by javascript [closed] - javascript

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() {});

Related

Node JS Socket.IO and HTTP requests [closed]

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 ;)

How can I securely send a request from client to server [closed]

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 2 years ago.
Improve this question
Hi there guys I am using Flask for my backends so if I wanna send a request from client (js) to my backend (flask) I will normally use fetch()in js to send a request and I receive in my backend but the problem is that using Chrome developer tools all the guys are seeing to where I am sending a request and like I am geting tons of unwanted request to my backend which I not sent by my client(js) so guys are sending any way to prevent it
I have also tried API auth but the problem is they are seeing my API keys and sending request please help me
The browser belongs to the user. It is completely under their control.
It is impossible to send data from the browser without the user being able to inspect it.
You can't secure the browser from the user who owns it.
There is no way to restrict an API so that it can only be accessed by your code running in the user's browser.
While every thing on the client belongs to the client , HTTP response headers can be leveraged to tighten up the security of web apps, typically just by adding a few lines of code. Depending on the technology your using

How are websites actually made? [closed]

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 7 years ago.
Improve this question
Every where I look, I see that websites can be created in several different ways. Some sources say that you have to use HTML and add Javascript, but there are others ways I have seen, like using Node.js(how can this thing create servers?). Apparently there are a multitude of ways to make websites. How do these different methods(Node.js) create websites, and what is different from just using HTML and adding scripts to it? Additionally, how do these websites become available to the public after creation? I'm interested in what goes on to create web servers and get them public, not what to write to make it happen.
Everything you see in your browser is written in HTML/CSS. Your browser can display dynamic features by running JavaScript, as you said.
How this HTML and CSS is generated is where the various methods come into play.
An accessible web server will respond to any received requests (the internet is just a huge network). The web server looks at what resource was requested and will respond appropriately. This might involve running a PHP script, hitting an entry point in a C# application, or triggering an event in a Node.js application to name a few actions.
These actions all have something in common: they will result in some plain HTML/CSS/JavaScript/data being handed back to the web server, which is then sent back in the response.
In some cases, the HTML/CSS might be so simple that you can hand-write it and that's all there is to your website. Lots of websites have been written this way. No scripts or application needed (i.e., no PHP, Python, Node, C# etc).
However, if you want your website to be dynamic (e.g., simply display the current date and time) or big or complex or data-dependent, you wouldn't want to hand-write it. In this case, you would write some application or script on the server to generate the necessary HTML/CSS for you.
To make a website public, you can just rent a hosting service and a domain name at a provider - google for "web hosting providers". You will be able to upload your files using FTP protocol and manage your database (usually MySQL) over some tool (most used is PhpMyAdmin).
I'm going to simplify what happens in the background.
The server needs to be connected to internet 24/7 (since the content it's hosting is available only when the server is connected).
Optionally (but very recommended), domain records need to point to the server's IP address(es)... Let's say your server has an IP address 12.34.56.78 - when you rent a domain called "example.com", you need to set its DNS record to point to this IP address so anyone who types in "example.com" into their browser, will communicate with your server.
The server software will see that someone is trying to communicate with it, requesting "example.com" on port 80 (http) and it will return content that has set up to return for this kind of request.

Are websockets the right technology to be used to update progress bars for the client and how to implement it? [closed]

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

How do I improve a chat application to be less taxing for the server [closed]

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

Categories

Resources