java script server async events - javascript

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.

Related

Connection stream

Is there another way to create a connection stream between a loaded page (client) and the server without using repeated AJAX calls, like every half second? I think of something that will make the client listening until the server gives a response (which can take a few seconds to arrive).
Thank you for your answers.
I would like to recommd you Phil Leggetter's Guide to Real-time technologies - because that is what you are looking for.
You will need one of the html5 introduced connection aspects: WebSockets, Server-Sent Events or even WebRTC DataChannel could be possible if you have a peer-to-peer use case.
In reference to the above link, its always about your server setup and your use case - that in mind some commonly used technologies would be: Pusher, Firebase, Socket.io, Ratchet, etc.
Update
Server-sent-events do have more server-side compatibilitly, technically it is more like optimized HTTP streaming, you can read more details in the spec and on caniuse.
If you need a constant bidirectional connection and better client side support you better go with WebSockets. But you will need to set up a compatible server or use an external service like Pusher or Firebase.

Is there a browser-based Websocket listener implementation?

I am interested to know if anyone has built a javascript websocket listener for a browser. Basically the server side of a websocket that runs in a client. This would allow messages to be sent to the client directly. Why? Because instead of having a Node.js, python, java, etc, server process sitting on or near the client/browser, I can just use a thread in the browser as a listening server thread. I don't think that any browsers support this currently.
I've run across answers like this: https://news.ycombinator.com/item?id=2316132
Just curious if anyone has done this. I believe that the current Websockets spec does not support listeners on the browser. It would make the deployment of various peer-to-peer applications a bit easier to deploy.
WebRTC allows for peer-to-peer connections to be made between browsers.
You would still need a server in order for individual users to discover each other but then they could connect directly to each other rather than having to pass all their traffic via a central server.
The idea.
You can use a simple echo server written in any language. Your script can send the data to the server then get it back, handle it on the same page with different functions/classes emulating the real server.
An example: http://www.websocket.org/echo.html
Then, you can think about different formats of packets to/from server to diffirentiate them inside one script.

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

Javascript remote events?

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

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