Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/justinribeiro/jquery-websocket-callback
jQuery plugin that implements WebSockets with $.callbacks()
https://github.com/justinribeiro/jquery-websocket-callback
Last synced: 7 days ago
JSON representation
jQuery plugin that implements WebSockets with $.callbacks()
- Host: GitHub
- URL: https://github.com/justinribeiro/jquery-websocket-callback
- Owner: justinribeiro
- License: mit
- Created: 2014-02-09T09:20:43.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-02-09T10:01:33.000Z (almost 11 years ago)
- Last Synced: 2024-10-23T23:58:21.954Z (28 days ago)
- Language: JavaScript
- Size: 116 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jquery-websocket-callback
A jQuery plugin that implements WebSockets with $.callbacks!## Example
Let's say we want to listen for messages. We can do so quickly by creating our $.websocket and then subscribing to a set of particular topics:```javascript
var myBrokerEcho = $.websocket({
url: "ws://echo.websocket.org/"
});myBrokerEcho.topic( "websocket.onOpen" ).subscribe( onOpen );
myBrokerEcho.topic( "websocket.onMessage" ).subscribe( onMessage );
function onOpen( value ) {
console.log("Connection Open!", value);
}function onMessage( value ) {
console.log("Incoming Message!", value);
}
```
## Why a WebSocket with a $.callback is cool
In short, the power of a pub/sub model with support for $.Deferreds. Let's rewrite our basic connect example and use a $.Deferred to send a message once we know we're connected:```javascript
var myBrokerEcho = $.websocket({
url: "ws://echo.websocket.org/"
});myBrokerEcho.topic( "websocket.onOpen" ).subscribe( onOpen );
myBrokerEcho.topic( "websocket.onMessage" ).subscribe( onMessage );
var dfd = $.Deferred();
var topic = myBrokerEcho.topic( "websocket.send" );
dfd.done( topic.publish );function onOpen( value ) {
console.log("Connection Open!", value);
dfd.resolve( "I'm resolved and sending a message that should echo back" );
}function onMessage( value ) {
console.log("Incoming Message!", value);
}
```
Oh so sweet.## Settings
The avaliable options are sparse at the moment.```javascript
var instance = $.websocket({
url: "ws://somewhere"
protocals: null,
debug: false,
heartbeat: false
});
```
## Topics
Default topics you can subscribe/publish/unsubscribe to are as follow:```
instance.topic( "websocket.onOpen" )...
instance.topic( "websocket.onMessage" )...
instance.topic( "websocket.onClose" )...
instance.topic( "websocket.onError" )...
instance.topic( "websocket.send" )
```## A work in progress
The base functionality works pretty well, but consider this version 0.0.1. I expect revisions and bug fixing. :-)