Client side scripting can get server side HTTP callbacks? - javascript

Background
I have coded a system that have two parts: server code (ASP.NET MVC + C#) and client side (AngularJS application).
The server side make a calculation of many factors and when it find a "solution" - it's fire a HTTP callback to notify the user.
The client side is presenting a data which found so far by the server. This is the visualization of the data as a user report.
What I have so far?
I currently have both side (server side + client side) coded. The way that HTTP callbacks is catched by the client side (step 3) this is the weak part which I think I should upgrade:
The data is visualize by AngularJS on the browser.
The server side find a solution (zero or more) and fire a HTTP callback to the webserver.
The webserver is getting the HTTP callback and notify the AngularJS app to update its view. This step is made by SignalR which giving me the option to trigger JS code after server event.
The Question
SignalR is giving me the ability to connect the client side (js) to server side events.
Is this possible to update the client side without using SignalR? Of-cause solution like "pull the web-server every N seconds for changes" are not recommended (performance).
Can you think about alternative to SignalR? Alternative system architecture?
Waiting to learn from you the best solution for this problem.
Thank you.

Related

Why in socket io they having set of script lines in html page?

I am trying to develop a flutter chat app using nodejs and socketio.i'm just getting started with the socketio i have surf several examples in which every front end or client side having a set of socketio script. Can i develop my code purely server side? so that i can integrate with any type of front end.
You can do that. Socket.io is a web socket implementation in node js. It enables real-time, bidirectional and event-based communication between client and server.
Server and client can send and receive the events. If you don't want write any script in client side then you don't have any use of sockets.
Edit:
socket.io given nice example for chat application with explanation. https://socket.io/get-started/chat/
Socket.io has two distinct parts - the server and the client. The docs make this clear. There is nothing preventing you from just writing the server part, and leaving the client implementation out of the equation, for someone else to implement.
Although the main point of Socket.io is to implement the WebSocket protocol, it does so by wrapping it in its own interface, so to speak. So a socket.io server can only talk to socket.io clients, not clients that implement WebSocket in other ways. That being said, there are socket.io client libraries for pretty much every major language out there:
Javascript (main client)
Java
C++
Dart/Flutter
You also asked about React and Angular, but both of those are web front-ends, so there should be no reason why they can't use the standard Javascript client library with those frameworks.

How to implement long polling for React + axios

I'm using React + axios to talk to the API from the client side. I'm a newbie in JavaScript.
How would I implement long polling so I get near real-time updates on a web page?
Is there a better way to do real-time updates on the page, when backend is a JSON REST API? Should I look into using WebSockets or server side events or long polling is fine?
There is another, potentially better way for your use-case: Server-Sent Events.
SSE, in a nutshell, is a simple GET request to the server from the client - except that the server doesn't close the connection after it's done processing the request. Instead, the HTTP connection is left open and the server is able to write data multiple times to the client, which appear in real-time.
For more info on how SSE compares to Websockets, read Alex Recarey's answer to "WebSockets vs. Server-Sent events/EventSource" in SO.

Alternate of server polling?

As we know, if running application also manage sessions in main memory then is there any way for server to send responses to all web clients/browsers for new recorded data in a database.
Remember: I have not made any request to server or polling to server for new records update..
Let server make responses without web request..
Objective :
No all web browsers making request or polling to server for every certain interval therefore reducing the performance issue with the application memory..
Am just against of making so many ajax calls from every web client..
Need your ideas from your past, if experienced similar..
read about websockets and socket.io.
basically with socket.io you have a connection open between browser (client) and server and server can send data which the client than receives as an event.
the client doesn't need to send a request to get that data, only open the web socket connection.
you can look at socket.io chat example: http://socket.io/get-started/chat/
WebSocket is the best and easy solution if you don't want to go through the hassle to learn Angular or others.
Both server-side and client-side can build WebSocket, and it acts as a bridge to transmit data back and forth.
I just created an easy solution for this.
Please check my new library wsm - WebSocket Manager, it works for both server-side and client-side.
Websocket Server can be built easily; this library includes several useful features.

Express web sockets and a central server

I'm creating an app where the server and the clients will run on the same local network. Is it possible to use web sockets, or rather more specifically, socket.io to have one central server and many clients that are running native apps
? The way I understand socket.io to work is that the clients read the web-pages that are served from the server but what happens when your clients become tablet devices running native apps instead of web pages in a browser?
The scenario I'm working with at the minute will have one central server containing a MEAN app and the clients (iPads) will make GET requests to the data available on the server. However, I'd also like there to be real-time functionality so if someone triggers a POST request on their iPad, the server acknowledges it and displays it in the server's client-side. The iPad apps will (ideally) be running native phonegap applications rather than accessing 192.168.1.1:9000 from their browser.
Is this technically possible to connect to the socket server from the native apps or would the devices have to send POST requests to a central server that's constantly listening for new 'messages'? I'm totally new to the whole real-time stuff so I'm just trying to wrap my head around it all.
Apologies if this isn't totally clear, it's a bit hard to describe with just text but I think you get the idea?
Correct me if I am wrong.
You have multiple iPads running native app. They send a POST request to your node JS server which is running in a computer in the same local network. Whenever the server receives a request from app, you want to display that a request has been received in your computer screen.
If my assumptions about the scenario is correct, then it is fairly easy to do. Here are the steps to do it.
Create a small webpage (front end). Load socket IO in the front end page like this -
<script type="text/javascript" src="YOUR_SERVER_IP/socket.io/socket.io.js"></script>
Then connect to server using var socket = io(). This should trigger connection event in your backend.
Handle all POST request from apps normally. Nothing special. Just add a small snippet in between. socket.emit('new_request', request_data). This sends new_request event to front end.
Handle the new_request in your front end using socket.on('new_request', function(request_data) { ... });. That's it. No need to add anything to your native app for realtime update.
The second step would be a little complicated as it is necessary to make socket variable available inside all POST requests. Since you chose node.js, I don't think you need any help with that.
Not totally clear on your project, but I'll try to give you some pointers.
An effective way to send data between native apps and a server is using a REST server. REST is based on HTTP requests and allows you to modify data on the server, which can connect to your database. The data returned is typically either JSON or XML formatted. See here for a brief intro: http://www.infoq.com/articles/rest-introduction
Android/iOS/etc have built in APIs for making HTTP requests. Your native app would send a request to the server, parse the response, and update your native UI accordingly. The same server can be used from a website using jQuery ajax HTTP requests.
Express.js is more suited to serving web pages and includes things like templating. Look into "restify" (see here: mcavage.me/node-restify/) if you just want to have a REST server that handles requests. Both run on top of node.js (nodejs.org).
As far as real-time communication, if you're developing for iOS look into APNS (Apple Push Notification Service). Apple maintains a persistent connection, and by going through their servers you can easily send messages to your app. The equivalent of this on Android is GCM (Google Cloud Messaging).
You can also do sockets directly if that's easier for you. Be careful with maintaining an open socket on a mobile device though, it can be a huge battery drain. Here's a library for connecting ObjC to Socket.IO using websockets, it may be useful for you: https://github.com/pkyeck/socket.IO-objc
Hope that helps!
To answer your question, it is definitely possible. Socket.io would serve as the central server that can essentially emit messages to all of the client. You can also make Socket.io listen for the messages from any of the clients and serve the emitted message to the rest of the clients.
Here's an example of how socket.io can be used. Simply clone, npm install, and run using 'node app.js'
All you have to do is to provide a valid server address when you connect your socket from the iPad clients:
var socket = io.connect( 'http://my.external.nodejs.server' );
Let us know if you need help with actual sending/receiving of socket events.
It is possible to connect to Websockets from your apps.
If you are using PhoneGap then you need a pluging that gives support to websockets in your app (the client) and then use websocket like normal way using Javascript see this.
If your app is native iOS look into this it could help you.
The primary use of the Sockets in your case is to be a bidirectional "pipe" between an app and server. There is no need of server sending the whole web-page to the native app. All what you need is to send some data from server to the client(app) in response to POST (or GET) request and then using this data on client side to update client's UI in real-time. If you are going to use moderate amount of devices (say tens of them), you may have connected all of them to the server permanently keeping individual socket connection open for every individual link server-to-app. Thus you may deliver data and update client's state in real time.
In fact web browsers also employ sockets to communicate to web servers. However as in general case there is no control on amount of concurrent clients in Internet, for the sake of limited networking resources conservation, servers do not keep sockets open for a long time, closing it just after the web-page was sent to client (or timeout has expired). That's how HTTP protocol works on the low level. The server waiting for the HTTP clients (browsers) by listening the 80 port, responding them by sending the whole web page content, then closing the connection and keep waiting for another requests on the same port.
In your case it's basically a good idea to use socket.io as it's a uniform implementation of sockets (ok WebSockets) on both client and server side. The good starting point is here

node.js executing server side function from client side

I have the server.js file on server side and the index.html on client side. I need to call a function from the server side through maybe a button or something on the index.html and get a feedbak on the index.html that the function was executed.
In the future I would also like to pass over a variable to the server side from client side.
I'm rather new to node.js. Hopefully someone can help me on this.
Node.js is no different than any other server side technology: it's not client side.
If you want to execute something on the server, you have two ways:
Submit a form. This will reload the page and you can choose to redirect on the same page it was.
Run an ajax request from the client side to do something server side.
A third way (thanks to socket.io) is using websockets for your purpose. I wouldn't suggest that you use them if you don't know what it is, though. Learn HTTP, then websockets.
This is a great question. I have done this using Socket.io (http://socket.io/)-- which is a nice library that allows sending data from the client (index.html) to the server, and allowing the server to send data to the client.
Look at the examples on socket.io site.
Well, to make it simple, your function should be "exposed" through some URL. You can use http://expressjs.com/ framework to speed up server side application creation. Once you have that and your function is available at let's say http://localhost/myFunction?myParameter=myValue, you can use jQuery call $.json() to invoke that function from your client side application, i.e. your index.html.

Categories