HTML and CSS Reference
In-Depth Information
Another common use for callbacks is as events. Whenever a DOM event is fired, it's using
a callback pattern. A function is provided as a parameter or property to indicate that when
specific things occur, such as a mouseover , to call back to the specified function to run some
custom logic related to the end-user action.
Many APIs that JavaScript and the browser expose as part of the HTML5 specification
involve the use of callbacks. In this objective, the WebSocket API is examined. Also, the use of
jQuery is introduced as it applies to making Asynchronous JavaScript and XML (AJAX) calls.
The ability to wire up an event and implement a callback using anonymous functions is also
discussed. Finally, this objective covers the use of the this pointer.
This objective covers how to:
Implement bidirectional communication with the WebSocket API
Make webpages dynamic with jQuery and AJAX
Wire up an event with jQuery
Implement a callback with an anonymous function
Use the this pointer
Implementing bidirectional communication with the
WebSocket API
The WebSocket API provides bidirectional communication support to your web applications.
WebSocket has greatly simplified the way data can be sent and received. Traditional methods,
such as long polling, have existed for a long time and are widely used all over the web today.
However, traditional techniques use the heavier HTTP mechanisms, which make the applica-
tion inherently less efficient. The use of the WebSocket API allows the connection directly to
a server over a socket. This is a much lighter weight connection and is fully bidirectional; both
binary and text-based data can be sent and received.
The full implementation of a WebSocket API requires that a webserver have a proper
server-side implementation that can accept socket connections. Technologies such as
Node.js work well for this purpose. Implementation of such technologies is beyond the
scope of this topic, and these code samples assume such an implementation exists.
The use of the WebSocket API is ideal for real-time applications such as messenger/chat
applications, server-based games, and more advanced scenarios, such as WebRTC (Web
Search WWH ::

Custom Search