Javascript remote events? - javascript

Is it possible to have a web service notify web clients when something occurs via HTTP request/response? Right now my client has to poll the server using HTTP Requests at an interval to check for updates, but it would be far more convenient if I could register a javascript function to the server from my client and have it be simply called when server state changes.
Note that the web service is written in Python and utilizes HTTP APIs (I think it uses cherrypy, if that's relevant).
If this is possible could someone point me to some tutorial that explains how to do this or give me a basic understanding of how this can be accomplished?

Look into Comet

You can't start a request from the server to the client. You can only poll the server (which you did), use a hidden iframe, use a plugin, or use the new HTML5 WebSockets which allows the server to send a message to the client.

If you are into Node.JS, look into socket.io and hook.io
https://github.com/hookio
https://socket.io

Related

Socket server on client side js?

I have node.js server, and I need to create dynamically updated web page with updating data. So, I thought that sockets are way to go.
But there's one problem. I need to send data from the server to client(to browser).
From my research, it is not really possible to create socket server with the client side JS. It is easy to do it the other way, but to send data only from server to client?
What would be best and easiest way to do that?
You create a webSocket or socket.io connection from your client to your server and your server is the webSocket or socket.io server. Once that connection is established, you can then freely send data either way across the connection, from client to server or from server to client.
This type of architecture is ideal for sending data from your server to a web page to dynamically update the web page as new data arrives.
webSocket is the base transport. socket.io is a layer on top of webSocket that adds a bunch of useful features such as auto-reconnect and structured messages. You can use either from a browser. webSocket support is built-in to the browser. If you want to use the additional features of socket.io, then you include the socket.io client library in your web page.
Here's a listing of some of the additional features socket.io offers over a plain webSocket: Moving from socket.io to raw websockets?.
I am not sure I have fully understood your question.
But, if I got it correctly, in order to have a "socket connection" you need to have two sides - a server and a client.
Use socket.io lib with a lightweight node.js server.
You can take a look at their docs + examples - will be very straight-forward.
If you still having trouble, write.

java script server async events

I have a web server which I want to send data to the client when the server detects a change in the data base. I want the client to receive the data without polling.
What is the best way to achieve this?
(I read a bit about SSE - server sent events, but not sure this is the way to go)
Thanks
Yes, Server-Sent Events is appropriate technology for listening to changes on the server.
If you're only listening, then SSE is better (faster, lightweight, HTTP-compatible) than a 2-way WebSocket.
If polling is not an option, you can think about the better technique, WebSockets, if your server supports it (and your target browser!).
One way or another, you need a connection opened by the client to the server, though.
The answer would vary depending on the freedom you have with client-side Javascript.
If your app gives you complete freedom, you could even set up your client to be a JMS listener and communicate JMS messages from client to server. For ex, you can use Apache Ajax.
Another option is to set up your web client to be redis client using, for ex, Webdis
As you have mentioned, Server-sent-events is an option too, so is WebSockets.
You need to choose based on your constraints.

library for client-side events (asp.net)

anyone knows some good libraries to implement client-side events which are created by the server?
e.g.
Client starts an action over webservice and then server send events to the client when the state has changed.
thanks
Knockout is free open source client side library.
It might be worth taking a look to
http://knockoutjs.com/
Http currently relies on Request/Response so once you get the response back from the web service you cannot trabsfer more information of which event to raise
i think what you want here is Sockets so that once connected you can transfer data multiple times from server to client and client can take decisions accordingly
1 library of sockets that i use is SignalR though it uses LongPolling but switches to sockets when they are available

Using Node.js to track XMLHTTPRequests

I've just started learning Node.js and was very interested in its real-time capabilities, especially with Socket.io. Since then, I've written a very basic script to connect to Twitter's streaming server and broadcast tweets to all connected clients.
To build that, I used http.createClient to connect to stream.twitter.com and added in the relevant response and data event handlers. Everything works quite well.
Obviously, Twitter's Streaming API pretty much outputs an infinitely loading webpage and what why using a data event handler works fairly well with it. However, is it possible to make other types of websites 'streamable'?
For example, if a client (browser) updates a website periodically using an XMLHTTPRequest, would it be possible to track the output of those requests using the HTTP API of Node.js? Or similar Node.js extension?
Thanks.
websites do not periodically use XMLHTTPRequest. Clients periodically send XMLHTTPRequests to an URL.
A simple call to http.request(options, callback) with the correct headers should emulate XHR's. Most of these servers will also accept normal POST or GET requests.
If you want node.js to connect to a server and simulate a browser then something like zombie would serve you well. It claims to support XMLHTTPRequest.
The best case for you would be to use web-sockets between your dashboard and node server. This way node will be notified immediately that something has updated at your dashboard ( I am assuming that you can modify your dashboard a bit to accept such connections, won't be difficult as long as you have access).
Then you can use long polling at client-end i.e. send a request to the node server and wait. Node will receive the request and then register an event to it. The moment it receives the updates from dashboard, it'll fire the event which will send the response to all the clients one by one waiting.
I would recommend take a look at http://github.com/andrewdavey/vogue . It does something similar but the functionality is ofcourse different.

callback javascript

I write a browser game (php, javascript) and I do not know how to make a callback. necessary that the server itself
found a client and call the function (only had one)
Don't write a browsergame if you don't know the basics! Browsergames are way too complex to learn programming.
If you want to make the server notify a client about something you will need to keep a connection open (search keywords: COMET, long polling) as you cannot initiate connections from the server to clients.
For this I can suggest you using Firebase. It is a API that let you to add cloud Data management that your user clients do. You can use that communication to search for client.
If I understand your question, what you need is a socket. Since you're using PHP and Javascript, a WebSocket might be just what you're looking for. With WebSockets, the connection between the client and the server is persisted, so the server can just push data/messages to any or all of the clients connected to it at any point in time. Likewise, the any client connected to the server can push messages/data up to the server.
Here's a video that describes how it works a bit https://www.youtube.com/watch?v=oJxWhmt5m-o

Categories

Resources