What are alternate usage of websockets (except comet)? - javascript

I know websockets provide a better solution for comet (reverse Ajax, often done with long-polling).
Can we use them for something else?
Example:
- Can we use them to communicate between browsers?
- Can we use them to communicate with a Java application (instead of using an applet)? Or to another application (rather than using Flash)?
- Can we use them to communicate with another server than the one that served the main page (cross-domain)?
- Other types of usage?

Can we use them to communicate between browsers?
The server can fetch data and flush it to someone else, so that you can send data from one client to another. So yes it's possible, but not directly.
Can we use them to communicate with a Java application (instead of using an applet)? Or to another application (rather than using Flash)?
I'm not completely sure what you mean with "Java application", but the server can basically do everything at its side.
Can we use them to communicate with another server than the one that served the main page (cross-domain)?
Sure, that's one of the interesting parts of WebSockets.
Other types of usage?
There are tons of opportunities created by WebSockets, one of which being the fact that you can create a multiplayer game, which is certainly interesting.

Related

Is this WebSockets approach correct?

I want to create this routine:
I access the /receiver (Receiver)
Receiver is listening for 'hello' event
I access the /emitter (Emitter) from another tab
Emitter fires the 'hello' event
Receiver says alert("Hello world") when 'hello' event is fired
Is it possible using WebSockets? I want to make the API server with Python, and the client with JavaScript.
webSockets connect client and server. The do not directly connect two web pages in two different tabs.
It is possible that two web pages in two different tabs could each connect to the server and the server could then route messages sent from one web page to the other web page. That's how a typical chat program works (which is a classic demo app for webSockets).
Yes, this is possible to build with a server in Python and client in Javascript web page.
You can certainly find many libraries written for webSockets in Python with your own search. Browser Javascript already has webSocket support built in. Many people choose to use socket.io which is a higher-level library built on top of webSocket and there are implementations for socket.io in many languages (including Javascript for the browser and Python for the server).
WebSocket is used when you need a persistent, web-friendly connection with or without a browser. If you just need to merely communicate among tabs in the same browser instance, you can use localStorage (which fires an StorageEvent event) even if you are offline.
If you potentially need the emitter to be accessed by another browser out on the web, or if the emitter was not a browser web-app (e.g., an IoT use case), then you would need WebSocket. Then one good solution would be a simple publish/subscribe mechanism using WebSocket. Here's a good Angular library that a colleague wrote that might help you:
https://github.com/kaazing/tutorials
Full disclosure: I work for Kaazing

How to add html/js query to gwt app using gwt-rpc?

We have a web client built with GWT that talks to a server via gwt-rpc. One part of the app will now use html/js to replace the GWT GUI and we want that part to talk to the server but not via gwt-rpc. What ways are there to migrate a gwt-rpc call to something that will work with a none a gwt client? Today we send and receive java collections that contain object graphs like a list of order objects that have them self order detail objects and so on.
Thanks
Your best bet is using REST. Your data is sent using JSON, which is part of JavaScript so it will always be understood on the client-side. On the server side there shouldn't be any problems finding a suitable library to handle the rest (ha-ha, get it?).
If you're using GWT on the client and server side for now, resty-gwt and Jersey are a popular combo. There was a talk about it on the last GWT.create conference, it's a good starting point.

How to use js to control actionscript on another page?

I know it's pretty easy to connect flash and js on one page by using external interface call.
But how would you connect a flash with the js on another page?
For example, if I had a flash game opened on one computer, but the user can only interact with the game with js on another computer. \
Please help.
I don't think you would be able to get these two things to communicate separately without some kind of medium or using XML Sockets. If it doesn't need to be real-time, then consider sending/receiving data to/from a MySQL server. If it does - I know you'll need to look at XML sockets - I'm just not 100% on how they are used as I've never had the resources to experiment.
I don't thinks this is possible. Only using some server-side programming ou multiplayer server, etc.
Using LocalConnection your SWFs can comunicate to each other, but they need to stay in the same page, same user.

PostgreSQL sockets from JavaScript (HTML5)

I'm looking at options to connect directly--without a web server or middleware--to a PostgreSQL server using JavaScript from a web browser client. On github, I found three projects:
node_postgres
node-postgres
postgres-js
They all appear to be in early but at least somewhat active development.
Do they all do roughly the same thing? Is what they do even what I'm looking for? Does anyone have experience with any of them that could recommend one over the others?
node-postgres was inspired by postgres-js and does roughly the same thing.
However, they both seem to be their own sort of middleware, because they require node.js, which is a server-side JavaScript implementation of a web server. So they would cut out a layer, but still not be the same thing as connecting directly to the PostgreSQL server.
There might be a way to combine the code in them with some HTML5 socket examples, though, to make connections directly from a web browser client.
If you are interested in CLIENT side JavaScript, as the OP's question implied, but you don't insist on owning the server, there is a commercial service that can help you.
The Rdbhost service makes PostgreSQL servers accessible from client-side JavaScript. There is a security system to prevent unauthorized queries, using a server-side white-list and an automated white-list populating system.
It uses plain old AJAX style http requests, provides a jQuery extension to facilitate the querying.
See https://www.rdbhost.com .
There is no secure solution today. One of possible solutions would be htsql:
http://htsql.org/
However there you use web addresses to query, even with https your queries will be plain text!
You should/could use a small webserver to handle requests. Alternativelly you can write an app, or use a local postgres server to handle the connection (in this case you still will need some kind of webserver).
The problem is very simple: your webbrowsers are limited in protocols to talk to the web, and postgres is not on this list. In fact you should not try to overcome this issue, using a server-client architecture is a very good solution. Format your request with JS to make it as small as possible, and let your web-server scripts interpret it into functional sql requests. The answer can be parsed into shorter response, then a sql data transfer, and you just need to interpret it on your side. Since you will create interperers on all sides, you will achieve a higher abstraction then in case of direct db connection, and thus independency towards the backend engines you use.

AJAX and Client-Server Architecture with JavaScript

I have to program websites, but I rather don't like the static HTML nature. I prefer more of a client-server architecture.
Now I've figured, that with XMLhttp, you can basically dynamically update your page and send/request for information/action to/from a server. So this would basically cover the client area.
But to complete a client-server architecture, it is necessary for the server to send/request information, too, without being queried.
Is there any way, for example for a chat server, to send back a received message to all clients (the clients use a web browser) without that the clients have to query in a fixed interval? I want to implement that one can see while you type something in.
There are several different ways to accomplish this. Some of them are already answered here, but I wanted to include a few more as well as my thoughts on them.
1. Polling
Frequent requests are made to the server to check for new info. This is the worst way to do this, but probably the easiest. If your site will have a low number of users, it might be worth doing it this way.
This can be accomplished by using setInterval(myFunction, n) in javascript to send XMLHttpRequests to the server every n milliseconds. Then, on the server, you respond to this with your new info, when you have it, or some message that implies no new info.
2. Long Polling
When the page is loaded, it makes a request to the server for new info. The server holds the connection open until there is something to send back. This method reduces the amount of network traffic used, but increases the resources used on the server. You can use this for a small number of users, but it doesn't scale very well.
The easiest way to do this is to have the page that handles the AJAX request simply wait for new information to be available, then respond. This can tie up a lot connections on your server. So, use with care.
3. COMET
COMET is basically long polling, but the server is setup properly for it. It knows that these connections aren't "real" and it uses less resources to handle them. This is a great solution for this problem, but it requires that the server is explicitly setup for this purpose. There are COMET servers and COMET addins for other popular servers, but it will require some setup and sometimes some money.
Implementing this on .NET isn't the easiest thing in the world. You can pay for solutions, try to find someone else's code that does something similar, or try to write it yourself. I've not found any decent free solutions for this. If someone else has, please comment.
4. RIA
Another solution would be to include Flash, Silverlight, or Java Applet on your page. People often do this by using a 1x1 object so that they can use Flash or Silverlight to talk to the server. If you don't mind adding the dependency, this is a decent solution. If you already know Silverlight or Flash, it could be relatively simple to implement.
You can find tutorials on the internet for each of these options.
5. Web Sockets
If you are on the cutting edge, you can look into Web Sockets. It's only available in the latest builds of modern browsers. It was part of HTML5, but it might be its own spec now. Regardless, it means that older browsers won't be able to handle it. But, if you don't mind limiting yourself to the latest of browsers, you can use this amazing feature.
I believe that Chromium is the only browser that currently supports it. However, there is work being done to implement this in Firefox and WebKit.
I'll spare you the controversy and simply say that this does exactly what you want it to. The Abstract of the spec says it all.
This specification defines an API that enables Web pages to use the Web Sockets protocol for two-way communication with a remote host.
Special Mention
If you are interested in the world of Node JS, you can't go wrong with Socket IO. It will implement the best of whichever technology is available to the browser.
Conclusion
The best option is Socket.IO on Node JS. However, for an ASP.Net solution, go for COMET or Web Sockets, if you can. Otherwise, using Flash/Silverlight isn't terrible. Finally, polling and long polling should be last resorts. You could always support one of these, then fall back to another if there isn't support for it in the client's browser.
Yes, you can use COMET.
The client has to tell the server when the client-user begins typing. You've got a couple options here.
Frequent requests from the server for the latest activity. This would be taking place for each user involved in the chat. The same request could be used to send user-specific activity to the server as well: "Jonathan is typing..."
Long-polling. This essentially requests information from the server, and the server keeps the connection opened until it has something to send back. So your requests are minimized, but your connections stay opened longer.
Depending on your traffic, type of data being transmitted, server-environment, and many other factors, one of these options may shine more than the other.
You can use Silverlight for push notifications. Look at PollingDuplexHttpBinding. Since you are using ASP.Net MVC, adding Silverlight will be easy.
Look at this page for more information.
Based upon the REST architecture the html system is based upon, the servers role is to simply act as a resource for the client to pull from. I am generalizing but there are tools to implement this type of action on the client side, rather than on the server side.
You are better off writing/using a library that can request updates from the server periodically. You can encapsulate these types of requests in a javascript object that can fire events. This way your client side script can act like it's getting notified from the server. Review some common stuff with COMET you can probably find some tools to help you client side code.
HTML 5 has some tentative attempts at this type of functionality, but if you want your app to work on older browsers, your better off using more stable methods, like AJAX requested updates.

Categories

Resources