Local communication through online application

Asked

Viewed 950 times

2

I am developing a system for ordering a snack/pizza shop. Let’s assume that the waiter makes the order with the Tablet (Android) at the customer’s desk or the customer goes and makes the request at the reception (Windows). Depending on the request, it should be printed in a different printer, for example: if x-salad is ordered it should be printed in a printer in the "sector" of the corresponding kitchen and if a pizza is requested it should be printed in another "sector" that corresponds to the pizza.

Could someone point me out some article, or commands in order to time print on a particular printer and time on another printer? There would have to be an encoding for each operating system or the same would be for both?

Note: Printers are all Epson thermal printers.


I’m auditioning for the Windows, but in the Android, how could do printing management? It would also be interesting in Windows Phone, because the focus is attendance waiters with a tablet with lower cost, independent operating system.


The proposed architecture is this, where each Tablet would have the printers installed and from them the print command came out according to the type of request. All stations would access the system via internet (without server inside the establishment), where nothing in relation to the system would be necessary to install in the stations besides the printers.

My focus is to work with prints and printers via Android.

inserir a descrição da imagem aqui


As suggested, and by the way I found good suggestion, I will use another architecture, as shown in the image

inserir a descrição da imagem aqui

The new question is how I can send data from an online application (using Tablet) to a local server?

In principle I thought sending using AJAX because it seems to be a simpler solution, but so far I have not found reference to how to send AJAX request to local server through an online application.

3 answers

2

If the communication with the server is sporadic, and/or initiated by the user (for example, by clicking on an Update button), you must implement the search of the data in the client using AJAX, in this case it is not a communication towards SERVER <- CLIENT, but rather CLIENT -> SERVER, however, with AJAX, it happens without reloading the page and the interface remains fully responsive.

If you need real-time communication, for example, to implement a chat, or a multiplayer game, or an implementation of a collaborative interface, where users from different computers participate on the same screen (and this is orchestrated by the server)so the best is to use the new features for low latency communication between server/client.

Architecture for local and remote server application

To have an application that has centralized local functions (printing) and also a remote server that stores historical information, product registrations, etc. the ideal is to really break down the application. Below is a proposed architecture based on Android, local server ASP.NET Web.API 2/MVC 5, and remote server using Azure Apps API with SQL Azure database (you can modify this architecture completely to use Amazon AWS, or even a server provisioned by you).

Arquitetura proposta com Azure

In this architecture all components of the local network share the same Wifi (or ethernet network for the case of the local server that may be next to the router). The server exposes a webserver (in the case of Microsoft’s IIS) with an API developed in ASP.NET Web.Api 2. This server, in turn, communicates with the remote server by implementing in Microsoft Azure that exposes an API through the Azure API APPS service (which makes it too easy to manage and create Apis). In order to share as much code as possible between remote server, local and android app, the android app can be programmed in Xamarin in C# in Visual Studio, allowing you to reuse business model classes and transport data as well as API clients, since everything will be written in C#. Xamarin creates native apps for Android (iOS and Windows Phone), sharing over 95% of the code between them. The applications have the same performance as those written in Java on Android and Objective-C on iOS, as they are compiled natively at the end of the process, or dynamically in the virtual machine of android.

Web application communication with servers

The implementation of web communication with servers can be done in several ways. Below the most common.

The option in fact is Websockets.

A tutorial in English: https://www.html5rocks.com/pt/tutorials/websockets/basics/

For PHP, a library: http://socketo.me/

Official documentation: https://developer.mozilla.org/en-US/docs/WebSockets

Notes

However, even a chat client, or a collaborative application can work with pure AJAX and "Polling" operations (the client queries the server, for example, every 30s to see if there is new data). That’s how these things were done in the recent past.

Implementing real-time communication is not something simple. Usually beginners make so many mistakes that they give up and an implementation in "Polling" with AJAX can be simpler to start.

However, a low latency data update application is a marvel for the end user, as well as generating a lot of pride in who built it.

There are also options to Websockets, and abstraction layers on top of them too (which can decay gracefully to Polling or other simpler mechanism in browsers that do not support websockets, for example).

  • The communication would be sporadic after the user clicks a button on the application and it would pass the data to the local server. In the case doing in AJAX, would it be necessary to use something of the type no-ip, or just fixing the ip would be enough? My question would be to make the application online find the local server to pass the data.

  • @Andersonbrunelmodolon what do you mean as a local server? Your application is no longer connected to a server, where it is "served" and there runs its logic? Do you want it to communicate with another server? Which server is this? Explain your scenario better in the question! If just to send the data by AJAX to the server is trivial, I’ll even put an example:

  • Yes, it is just sending the data via AJAX from the web application to the local server. My question is whether just the server is on the same network of the device that accesses the application (let’s assume, with fixed ip) or I have to do something else either on the AJAX side or on the server side. The server is pure PHP that will be prepared to receive the data sent via AJAX.

  • @Andersonbrunelmodolon updated the answer with an architecture that I consider more robust (remembering that details of architecture can and should be adapted to your case and your knowledge list)

  • Some people messed up my question. I focused only on the communication to the fact that there is already a system in PHP practically finished, but without these new functionalities because the idea has recently appeared. It is difficult to use . NET in this context.

  • @Andersonbrunelmodolon strongly advise you to use android in the waiter’s app and not a Webview that shows a mobile site made in PHP. This will not work for the type of problem. Now, all the rest of the architecture I suggested can be replaced by PHP. Just publish a REST API in PHP (don’t implement an API other than REST!). A framework for this would be http://www.slimframework.com/ You can reuse the current PHP code in much of the API, as well as the existing database, etc.

  • In case you’re telling the local server to put something like a "no-ip" in order to be accessible via the web and work the communication between it the tablet with REST architecture? (AJAX Tablet => REST SERVER with no-ip)

  • @Andersonbrunelmodolon, nothing like that. Your local server is located on the local Wifi network (as I explained in the reply text). Your android app communicates with this server via its local IP (which should be fixed, or via DNS if your router allows it). In the app you must have an administrative area to indicate the local server IP. You then consume this API locally by indicating the local server IP. It’s very simple, there’s no need to access it remotely! Or this app will run OUTSIDE the local restaurant network?

  • The application will be hosted online and communication with Tablets is via internet Wi-Fi that has in the establishment. In case I just fix the local server IP (within the establishment), recording it on the Tablet and accessing via AJAX? The system to be online has an administrative area/ reports that is possible to access outside yes, but the part of the Tablet’s and communication with the server only within the establishment.

  • 1

    @Andersonbrunelmodolon if you create a web application inside the tablets and not an android application, then you will access the data via AJAX accessing the local server. Only the local server accesses the remote server (to search for new products, new prices, etc.). If you use an ANDROID application you will access the local server using a library to access the REST Apis, such as Retrofit (https://square.github.io/retrofit/). Even if you use an android website with AJAX, program the local server to expose a REST API, even if it is in PHP: http://www.slimframework.com/

Show 5 more comments

1

Considering that the main difference between the hosting server and your local server is the fact that one is online and the other is not, mainly due to its modem acting as a firewall, we can say that the basic solution would be:

  • Redirect ports on your modem so that you can access your local server over the internet (see Port Forwarding);
  • Configure your online application (hosted) to consume a service or url through the library CURL.

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://seuip:8080/teste.php");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    $response = curl_exec($curl);
    var_dump($response);
    

But attention.. I imagine this is just a joke. Because I see no reason in this world to expose your local server on the network.

  • In fact it is only a print server, and the fact of wanting to do via AJAX would be to not expose, because it is not known if in the future will come other needs with this server. But at first your idea is valid yes.

1


I decided to post something simple after observing the comments. Now I could have more confidence about which solution to present.

I always think of simple solutions so let’s go to a really simple alternative.

The question here is how to make the server "online" send data to the server on an intranet.

If we focus on this logic without looking at the real scenario, we will imagine inadequate and confusing solutions. Looking closely at the scenario, the Internet server does not need to send the data to the printers. This can be done by the tablet that is on the server page.

I thought it would be confusing to explain without illustrations, so I made some doodles.

First let’s give an overview of the scenario for a better understanding:

inserir a descrição da imagem aqui

The customer makes an order and the waiter enters the system by tablet.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

When completed, the online server returns a page with a hidden form with fields like Hidden.

The form action will point to the local server address on the intranet.

In this example, a pizza is being ordered. Then in the form will be specified the parameter "Printer" with the value "pizza".

<form action="http://192.168.1.50" method="POST">
<input type="hidden" name="printer" value="pizza">
<input type="hidden" name="data" value="os dados que deseja enviar para a impressora imprimir">
</form>

Note that the online server has no access to the local network of the restaurant, because this network is not open on the internet.

This communication is possible because the process takes place in client-side, by the client device Brower, the tablet browser.

Technically, it is the tablet that is sending the request and not the online server.

You can also implement a function that sends the form by ajax if you want something more "refined", but it will not make any difference in the final result.

inserir a descrição da imagem aqui

The hidden form can have a button for the waiter to send manually or can be triggered automatically.

inserir a descrição da imagem aqui

From now on I believe I don’t need to go into details because they are very simple things.

If the server is Windows, you can use the functions printer of PHP http://php.net/manual/fa/function.printer-open.php

You can also implement communication with printers using sockets http://php.net/manual/en/book.sockets.php

Simple as that, without fancy structures and monstrous costs. It’s efficient and simple to maintain.

*Sorry for the drawings.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.