Are there websocket libraries (like ajax for jquery) I can use on the browsers with fallback to ajax long polling?
I found that Socket.IO implements a weird, arbitrary layer over the WebSocket protocol. I'd rather just pass raw data back and forth, so for my project, I went with web-socket-js.
web-socket-js works similarly to Socket.IO in that it reverts to using Flash sockets if WebSocket support is unavailable. Thus, it works in all the major browsers that support Flash (I tested this myself).
Just make sure you open port 843 in your firewall or you'll get a Flash security policy error. You'll probably need a script to listen on port 843 as well...I used em-websocket for the socket server, and in its readme I believe is a link to a Perl script that provides this.
Library with WebSockets support and fallback long polling solution would probably depend also on server side technology and not only browser client. Try to look at socket.io for example.
I have been using SignalR for the last several months, and it is awesome. It does what Socket.IO does, but maybe even better. It degrades as follows: Web Sockets, Server Sent Events, Forever Frame, AJAX long polling. Only works with .NET though.
https://github.com/SignalR/SignalR/wiki/Faq
Yes, you'll need server support for that as well. Kaazing WebSocket Gateway supports a very fast emulation/Polyfill. If you have a plugin like Flash it may use that (opportunistic optimization), but if you don't it emulates WebSocket with encrypted streaming, which is a lot more efficient than long polling. It comes with SSE and Cross Document Messaging support and emulation as well, as well as many higher level protocol abstractions (JMS/Stomp, XMPP, etc.)
Union Server has WebSocket support with fallback to comet-style AJAX communication. Union is a platform for creating connected applications, such as online multiplayer games.
http://www.unionplatform.com
Related
I want to connect a bunch of weather sensors to a Raspberry PI. Writing the daemon that reads the sensors and writes the data to a database will be the easy part since I'm a systems programmer. I also want a simple cross platform UI for this device so I'd like to set my Raspberry Pi up as a WIFI hotspot that people can connect to and then just entering a URL like 'weather.local' into a browser which would take them to a web page where the weather sensor data is continually updated. I.e. I want the sensor daemon to 'push' updates to the web page.
The problem is that I'm no web developer. The solutions I can think of off the top of my head are:
Flash, which is out because I want this to work on mobile browsers.
Using Java script and hanging HTTP requests to a web service.
HTML5 socket.io which is presumably the same as WebSocket.
If I go with option (2) even if it's a kind of polling, I'll have to incorporate some form of HTTP server into my sensor daemon and I have a fair idea of how to code that. My question, however, regards the HTML5 socket IO. Can I use this to connect directly to a TCP/IP binary socket or do I need a server side WebSocket library? Also how widely is HTML5 socket.io/WebSocket implemented on mobile browsers?
WebSocket always begins with an upgrade handshake over HTTP, so you do need to have basic HTTP capability. It's simple enough that you can hand-code it.
WebSockets is basically supported by all modern browsers. It is not used that widely because it's a pain to set up on traditional HTTP servers and messes up with many proxies, but that's not a problem for you. As long as the client is a recent version of anything, it'll work.
A note about option 2: browsers have a native implementation of it — that means you don't need so much JavaScript on the client. You just create the EventSource object and listen to its events.
I want to stream real-time data from a server into a JavaScript webpage. For example, if the server receives meteorological data measurements every second, I want to visualize them as a moving graph on the webpage.
Naturally, Ajax can be used for this. However, this is not what the HTTP protocol was built for. It would help to have a faster, more lightweight protocol. Ideally, a persistent TCP connection. Are there any such possibilites in modern browsers, without using additional plugins and applets? Or can this be only done with Flash, Java, etc...?
I would check out Socket.IO. It tries to make use of WebSockets, but can fall back on standard AJAX.
Modern browsers support the Websocket implementation, however as David says, in case your browser does not support it, it will fall back to Flash sockets, Ajax, iframe long polling etc. It's a high level wrapper and easy to implement. Server side you will use nodejs and socket.io, check the documentation of socket.io
We have a network camera. It has an HTTP server to provides the current image. There is also a Telnet interface for controlling the camera (i.e. trigger, focus, etc.). I would like to add an HTML page to the camera that would provide a simple interface (we already have client software we write). I can "GET" the image and display that, but I would also like to have controls that use the Telnet interface to control the camera. So a button might have JavaScript code behind it that connects to the camera via Telnet (logs in) and issues the command to trigger the camera.
I know that JavaScript/browsers support connecting to the same host via XMLHttpRequest. In this case I would be looking to open a socket on port 23 and send text. I also know that I can do this through Flash, Java, or some other technology, but I would prefer to use JavaScript only. If that is possible.
Thomaschaaf is correct, while HTML5 introduces websockets you'll find they still require special server support as they post HTTP style information upon opening the socket:
JS/HTML5 WebSocket: Connect without HTTP call
The best way, currently, to have true sockets is to either
use a flash or Java component on the webpage that does the actual socket work.
use a proxy server with websockets that can handle the additional protocol overhead of websockets and connect to the real tcp/ip port with plain sockets.
The jsterm example Matt linked does the latter, and if your webcans are behind a firewall it will not work in your situation without also implementing another server.
There are libraries that implement the first method, two are linked here for convenience, many others can be found using a search engine:
http://stephengware.com/proj/javasocketbridge/ (Java)
http://matthaynes.net/blog/2008/07/17/socketbridge-flash-javascript-socket-bridge/ (Flash)
jsTerm is an HTML5 implementation of a Telnet client.
You'll need a browser that supports HTML5 WebSockets. WebSockets is the only method of doing non-HTTP requests with pure JavaScript.
Currently there is no way to do socket connections with JavaScript only.
But what you are searching for is a socket connection ;)
https://developer.mozilla.org/en/XML_Extras
If I interpret the question liberally as "is there a remote connectivity library for Javascript", then the answer is yes (quoting from https://xtermjs.org/):
A web based SSH2 client using xterm.js, socket.io, and ssh2: https://github.com/billchurch/WebSSH2
HTML5 Based SSHv2 Web Client with E2E encryption utilising xterm.js, SJCL & websockets: https://github.com/stuicey/SSHy
I've tried WebSSH2 with node.js briefly, it worked for me - I managed to connect to a Linux-based server with it.
(I know this probably doesn't help the OP but this is a 7-year old question anyway. Maybe it helps others who are needing an answer to a similar problem.)
To clarify when I ask about browser to browser communication I mean without a server in between forwarding message. I would like to implement something like this for a game. If p2p in websockets isn't possible are there similar alternatives? Any help is appreciated.
No. Browsers can only initiate WebSockets connections, not receive them. The W3C browser API spec only defines how to start an outbound connection.
You can make an application that would both initiate and accept WebSockets connections, but browsers do not do this.
You might look at Pusher App which you could use to build a WebSockets application with multiple clients. The service provides up to 20 simultaneous WebSockets clients for free (you can pay for higher scaling).
Update:
The WebRTC protocol and API is making rapid progress and allows a Data Channel to be established between two peers (you still may need a STUN/TURN server for the initial NAT traversal and setup).
In theory it is possible with WebRTC DataChannel:
RTCDataChannel is a WebRTC API for high performance, low latency,
peer-to-peer communication of arbritary data. The API is
simple—similar to WebSocket—but communication occurs directly between
browsers, so RTCDataChannel can be much faster than WebSocket even if
a relay (TURN) server is required (when 'hole punching' to cope with
firewalls and NATs fails).
"In theory" because it isn't supported by a stable browser yet and you still need a relay server (TURN) if one of the browsers is behind a symmetric NAT. Nevertheless, it is a really promising feature.
Update: Chrome 26 and Firefox 22 support RTCDataChannel by default and Firefox 19-21 if you enable WebRTC by setting media.peerconnection.enabled to true (about:config).
I was reading about websocket and peer 2 peer and found PeerJS.
I still haven't made anything though, but by the examples it looks promising.
Now days it's possible, currently only Chrome,FF and Opera support it (desktop).
There's some libraries starting to pop up around the web right now, such as PeerJS and js-platform-p2p which pretty much simplifies things.
Simple and reliable cross browser supported way is to use http://httprelay.io with AJAX calls. It is also implements one to many communication what could be useful for game development.
Is there a way to have a live connection (like for a chat server) with a server using only HTML(5)/JavaScript?
Apparently FF4, Chrome, and Safari all support Web Sockets.
Here's a firefox example, although I'm not sure the spec is completely finalized yet.
Due to the nature of HTTP (only clients can start requests), you would need a "Push" server (aka Comet) on the server-side. You'd still only need JS on the client. See:
HTTP Streaming for an explanation of the technology
APE: Ajax Push Engine for usable implementation
Comet Daily for news on Comet
This can also be implemented with a periodic refresh (polling) if you can't install a Comet engine on the server.
Yup. AJAX and some server code to handle messages updates is all you need to create just such a system. As #NullUserException noted, something like Comet might make this easier.