⬆️ ⬇️

Development of embedded JavaScript applications

Instead of intro



I want to tell you about some of the difficulties that we encountered when developing an embedded JavaScript application, the commentary widget.

In this article I will describe some of the problems and subtleties of developing such applications, and also offer options for solving them.

As a backend-solution, we use an application on Ruby on Rails , so some fragments of this article will be specific to Rails-environment.



The commentator consists of two projects: an API and a widget that is installed on the client's site. It will be about their interaction with each other and the widget with the client's site. Basically, the widget communicates with the API via JSONP , which is known to support only GET requests. In this connection, the first difficulty arises.





adding a comment



Since the comment can be quite lengthy, we cannot use the GET method for sending it, for example, due to restrictions on the length of the request in Internet Explorer, respectively, we can also forget about JSONP. We cannot send a POST – request to the application domain from the client’s site via AJAX due to restrictions imposed by the Same Origin Policy , but we can send the form using the POST method to an invisible frame.

The result of adding a comment can be obtained in different ways, for example, using easyXDM .

But we will go the other way and will use Pusher . We already use Pusher to instantly add comments to pages opened for all users, as well as to display it in the interface for moderators. It is much more convenient to use one tool to perform several tasks, especially since Pusher does an excellent job with them.

As a result, we get the following scheme: the user sends a comment to us to the server, we process it and transmit the result of adding Pusher – y, which in turn sends it to the user.

')





Pusher and Unicorn



We use Unicorn as a web server. Sending a Pusher – u event takes some time, which is not so noticeable when sending only one event. However, when you send multiple events, the response time increases significantly and becomes unacceptable. Unicorn does not belong to the evented – server family, which means that we cannot make sending events asynchronous.



The solution to this problem, proposed by the developers of Pusher, is to launch EventMachine in a thread, which delegates sending messages, next to each Unicorn process.



module Pusher module Async class << self def spawn Thread.new { EM.run } unless EM.reactor_running? end def respawn EM.stop if EM.reactor_running? spawn end end end class Request alias :send_async_without_next_tick :send_async def send_async df = EM::DefaultDeferrable.new EM.next_tick do send_async_without_next_tick .callback{ |response| df.succeed(response) } .errback{ |error| df.fail(error) } end df end end end 




About this solution a small article was written by the developers of Gauges , and there is also a ready-made solution .

Running an extra thread with EventMachine for each worker doesn't seem like a good idea to me.



The second option is to launch another small web server with Thin next to our Unicorn web server, which, without any problems, does its only task - to proxy requests to Pusher in such a way that sending the request to the proxy server takes time that tends to zero. And already the transfer of the request to Pusher by them is the second thing.



We used such a solution for some time, but later we refused from it, and we stopped on the third option - to send sending delayed job messages to the processor, in our case, by choosing Sidekiq for this. He instantly takes in the processing of tasks that appear in the queue, and is able to perform them very quickly.



Insert a widget with UTF-8 encoding on a page with a different encoding



Now that the problem with cross – domain queries has been solved, the next one is waiting for us - encoding. If you simply insert a javascript with UTF-8 onto a page with windows-1251, then all UTF-8 characters in it will be displayed incorrectly. To solve this problem, you need to add a charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
tag charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
  charset   UTF-8 . 
    



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
  charset   UTF-8 . 
    



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
  charset   UTF-8 . 
    



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .
charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .

charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .

charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .

charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .

charset UTF-8 .



<script src="http://example.com/script_in_unicode.js" charset="UTF-8"></script>



, "" , , . – , , , – @charset .

, , .



<style type="text/css"> @charset "UTF-8"; .breadcrumbs li:before { content: "β†’"; } .breadcrumbs li:first-child:before { content: ""; } </style>



, , , , β€” , UTF-8, , .



<form action="http://example.com/comments" method="post" target="hidden-iframe" enctype="application/x-www-form-urlencoded;charset=UTF-8" accept-charset="UTF-8"> <textarea name="body"></textarea> </form>



HTTP–

, , β€” , – .



, , , – X-Frame-Options , X-Content-Type-Options X-XSS-Protection , Content-Type .



. Firefox, , , Access-Control-Allow-Origin: *





, , , , , .



, , , .

Source: https://habr.com/ru/post/175753/



All Articles