summaryrefslogtreecommitdiff
path: root/www/lib/ng-websocket/README.md
blob: 946bea4c1392b213e6bb7473b8591b343a808f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
ng-websocket
============

**AngularJS HTML5 WebSocket** powerful wrapper module to develop with ease and fun!

# Index

  - [Introduction](#introduction)
  - [Requirements](#requirements)
  - [Installation](#installation)
  - [Usage](#usage)
  - [Tutorial](#tutorial)
  - [Features](#features)
    - [Lazy Initialization](#lazy)
    - [Auto Reconnection](#reconnect)
    - [Enqueue Unsent Messages](#enqueue)
    - [Mock Websocket Server](#mock)
  - [Testing](#testing)
  - [API](#api)
    - [$websocketProvider](#websocketProvider)
      - [$setup](#setup)
    - [$websocket](#websocket)
      - [$new](#new)
      - [$get](#get)
    - [ngWebsocket](#ngWebsocket)
      - [Constructor](#constructor)
      - [Constants](#constants)
      - [Events](#events)
      - [$on](#on)
      - [$un](#un)
      - [$emit](#emit)
      - [$open](#open)
      - [$close](#close)
      - [$status](#status)
      - [$ready](#ready)
      - [$mockup](#mockup)
    - [$$mockWebsocket](#mockWebsocket)
  - [Contribute](#contribute)
  - [License](#license)

# Introduction

**ngWebsocket** is a library that provides a provider and a service to handle **HTML5 WebSocket** with ease
in pure **AngularJS** style!
The idea behind this module is to give four kinds of object to handle websockets:

  - **$websocketProvider**: the provider is on top of usage. In fact, you can setup a general configuration for each ngWebsocket you're going to create
  - **$websocket**: following an Angular service that lets you to handle different websocket instance among your application
  - **ngWebsocket**: an instance of the HTML5 WebSocket wrapper (this is actually the core of this module): it provides lots of feature to work with websockets
  - **$$mockWebsocket**: this is a smart implementation of a websocket backend that lets you to developer and test your app without a real responding server

For each of these objects an API is available and fully documented in this document.

# Requirements

The only requirement needed is [AngularJS](https://angularjs.org/) that you can install it via [Bower](http://bower.io/).

# Installation

Use [Bower](http://bower.io/) to install this module:

```bash
$ bower install ng-websocket
```

Or simply `git clone` the repo and install the dependencies with [NPM](https://www.npmjs.org/):

```bash
$ git clone https://github.com/wilk/ngWebsocket
$ cd ngWebsocket
$ npm install
```

# Usage

After the [Installation](#installation), require it in your Angular application.

Firstly, in your `index.html`:

```html
<html>
    <head>
        <script src="bower_components/ng-websocket/ng-websocket.js"></script>
    </head>
</html>
```

Then, in your Angular application definition (assumed `app.js`):

```javascript
    'use strict';

    angular.module('MyApp', ['ngWebsocket']);
```

Now, you're ready to use it!

# Tutorial

Need to use HTML5 WebSocket to build your cool web application, huh?
No problem, dude! Check this out!

```javascript
'use strict';

angular.module('MyCoolWebApp', ['ngWebsocket'])
    .run(function ($websocket) {
        var ws = $websocket.$new('ws://localhost:12345'); // instance of ngWebsocket, handled by $websocket service

        ws.$on('$open', function () {
            console.log('Oh my gosh, websocket is really open! Fukken awesome!');

            ws.$emit('ping', 'hi listening websocket server'); // send a message to the websocket server

            var data = {
                level: 1,
                text: 'ngWebsocket rocks!',
                array: ['one', 'two', 'three'],
                nested: {
                    level: 2,
                    deeper: [{
                        hell: 'yeah'
                    }, {
                        so: 'good'
                    }]
                }
            };

            ws.$emit('pong', data);
        });

        ws.$on('pong', function (data) {
            console.log('The websocket server has sent the following data:');
            console.log(data);

            ws.$close();
        });

        ws.$on('$close', function () {
            console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
        });
    });
```

Easy, right?

Well, let's chain it!

```javascript
'use strict';

angular.module('MyCoolChainedWebApp', ['ngWebsocket'])
    .run(function ($websocket) {
        var ws = $websocket.$new('ws://localhost:12345')
          .$on('$open', function () {
            console.log('Oh my gosh, websocket is really open! Fukken awesome!');

            var data = {
                level: 1,
                text: 'ngWebsocket rocks!',
                array: ['one', 'two', 'three'],
                nested: {
                    level: 2,
                    deeper: [{
                        hell: 'yeah'
                    }, {
                        so: 'good'
                    }]
                }
            };

            ws.$emit('ping', 'hi listening websocket server') // send a message to the websocket server
              .$emit('pong', data);
          })
          .$on('pong', function (data) {
            console.log('The websocket server has sent the following data:');
            console.log(data);

            ws.$close();
          })
          .$on('$close', function () {
            console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
          });
    });
```

Your back-end team is lazy? No problem: we can do it on our own!

```javascript
'use strict';

angular.module('MyIndipendentCoolWebApp', ['ngWebsocket'])
    .run(function ($websocket) {
        var ws = $websocket.$new({
            url: 'ws://localhost:12345',
            mock: {
                fixtures: {
                    'custom event': {
                        data: 'websocket server mocked response'
                    },
                    'another event': {
                        data: {
                            damn: 'dude',
                            that: 'is awesome!'
                        }
                    }
                }
            }
        });

        ws.$on('$open', function () {
            ws.$emit('an event', 'a parrot response') // by default it responde with the same incoming data
              .$emit('custom event') // otherwise it uses the given fixtures
              .$emit('another event'); // even for objects
          })
          .$on('an event', function (message) {
            console.log(message); // it prints 'a parrot response'
          })
          .$on('custom event', function (message) {
            console.log(message); // it prints 'websocket server mocked response'
          })
          .$on('another event', function (message) {
            console.log(message); // it prints the object {damn: 'dude', that: 'is awesome!'}
          });
    });
```

# Features

ngWebsocket comes from Italy with lots of interesting stuff, folks!
Why not just a wrapper? Because we can do more with happiness and fun!

So, let's discover the awesome features list!

## Lazy

Using basic HTML5 WebSocket object, you experienced that the connection is open immediately, just after the websocket is created with **new** constructor.
By default, the same behaviour is used by ngWebsocket but you can simply change it with this powerful feature:

```javascript
angular.run(function ($websocket, $timeout) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        lazy: true
    });

    ws.$on('$open', function () {
        console.log('The ngWebsocket has open!'); // It will print after 5 (or more) seconds
    });

    $timeout(function () {
        ws.$open(); // Open the connction only at this point. It will fire the '$open' event
    }, 5000);
});
```

With [$websocket.$open](#open) function, you can open the connection when you want, especially after the coffee break.

**Default: disabled**

## Reconnect

Ok, your websocket connection went down due to a bad wifi connection and you don't want to make another connection
manually, right?
So, what about an automated feature that do this for you?

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        reconnect: true // it will reconnect after 2 seconds
    });

    ws.$on('$open', function () {
        console.log('Here we are and I\'m pretty sure to get back here for another time at least!');
      })
      .$on('$close', function () {
        console.log('Got close, damn you silly wifi!');
      });
});
```

With this feature, if the connection goes down, it will open again after 2 seconds by default.
If you need to get the connection back in fewer time, just use the **reconnectInterval** time slice:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        reconnect: true,
        reconnectInterval: 500 // it will reconnect after 0.5 seconds
    });

    ws.$on('$open', function () {
        console.log('Here we are and I\'m pretty sure to get back here for another time at least!');
      })
      .$on('$close', function () {
        console.log('Got close, damn you silly wifi!');
      });
});
```

**Pay attention, good sir**: if you close the ngWebsocket with the [**$close**](#close) method, it won't get the connection back
until the [**$open**](#open) is invoked!

**Default: enabled**

## Enqueue

From great powers come great responsability. Keep this in mind while reading this feature.

Sometimes, it would be useful if someone save our websocket communication, especially when the connection is down.
With this powerful feature, it's possible to store every unsent message in a queue and then flush them just the connection get up again.

How? Enabling enqueue feature, of course!

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        lazy: true,
        enqueue: true
    });

    ws.$emit('dude event', 'hi dude!'); // this message couldn't be forwarded because of the lazy property (the websocket is still closed)

    ws.$on('$open', function () {
        console.log('I\'m sure the above message gets sent before this log is printed in the console ;)');
    });

    ws.$open(); // when the websocket gets open, flushes every message stored in the internal queue
});
```

**BUT** this means that each message is stored into a memory queue and it can get really big, especially if your application sends many messages in a short time slice.

**Default: disabled**

## Mock

Dulcis in fundo, a websocket server implementation to use and test your application, without a real websocket server listening!
Yep, you well heard!

Think about this situation: you're developing the front-end part of your company application and the backend team is lazy (because every developer is lazy),
so you couldn't start writing your section because you need to send/retrieve data to/from the server.

No problem, you can!

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        mock: true
    });

    ws.$on('$open', function () {
        ws.$emit('hi', 'dude');
      })
      .$on('hi', function (message) {
        console.log(message); // it prints 'dude'
      });
});
```

By default, the mock feature simulate a parrot websocket server: this means that every message sent with
a certain event, will have a response with the same structure, with the same event and the same data.

However, you can setup some fixtures that simulate what your lazy back-end team is going to do after beer time:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        mock: {
            fixtures: {
                hi: {
                    data: 'dude, this is a custom message!'
                }
            }
        }
    });

    ws.$on('$open', function () {
        ws.$emit('hi');
      })
      .$on('hi', function (message) {
        console.log(message); // it prints 'dude, this is a custom message'
      });
});
```

**Default: disabled**

# Testing

This module uses [Karma](http://karma-runner.github.io/0.12/index.html) with [Jasmine](http://jasmine.github.io/) for unit testing, so before launching any test check out if all dependencies are correctly installed:

```bash
$ npm install
```

After that, launch the test:

```bash
$ npm test
```

# API

ngWebsocket APIs are composed by four different modules:

 - **$websocketProvider**
 - **$websocket**
 - **ngWebsocket**
 - **$$mockWebsocket** (private but configurable)

## $websocketProvider

Following the API of ngWebsocket Provider

### $setup

If you need to setup your custom default configuration for each ngWebsocket istance, pass it to this method:

```javascript
angular.config(function ($websocketProvider) {
    $websocketProvider.$setup({
        lazy: false,
        reconnect: true,
        reconnectInterval: 2000,
        mock: false,
        enqueue: false
    });
});
```

**Usage**

```javascript
$setup(config)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| config    | Object   | default ngWebsocket configuration |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| $websocketProvider | the $websocketProvider |

## $websocket

Following the API of the $websocket Service

### $get

Every ngWebsocket instance created with [$websocket.$new](#new) method are stored within the $websocket service.
To get one of them, you can use **$get** with the url of the websocket you're looking for:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$get('ws://localhost:12345');
});
```

The url is needed because it is stored using the url as the key of an hashmap.

**Usage**

```javascript
$get(url)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| url       | String   | the websocket url |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | an instance of ngWebsocket or undefined |

### $new

There are two ways to create a new instance of ngWebsocket:

**string (url)**

The url is always needed and it has to start with the websocket schema (ws:// or wss://):

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new('ws://localhost:12345');
});
```

A new instance is returned and the internal WebSocket has already started the connection with the websocket server on the backend.

**object**

All of the following configurations can be changed:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new(
        url: 'ws://localhost:12345',
        lazy: false,
        reconnect: true,
        reconnectInterval: 2000,
        mock: false,
        enqueue: false
    );
});
```

For more information see the [ngWebsocket Constructor section](#constructor).

**Usage**

```javascript
$new(url|config)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| url/config | String/Object | websocket url or a configuration set |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | an instance of ngWebsocket |

## ngWebsocket

ngWebsocket is the core of this module.
In a few words, it's a wrapper for the HTML5 WebSocket object, extending it with different features.
It acts like an EventEmitter and it provides a common way to attach a handler for each fired event.

Following the API in detail.

### Constructor

The constructor of the ngWebsocket accepts two kind of parameters:

  - String: the url starting with the WebSocket schema (ws:// or wss://)
  plus an optional String/String[] containing the protocols (this matches
  the WebSocket constructor API)
  - Object: a configuration containing the websocket url

The url is a requirement to create a new ngWebsocket.
An instance is always created with a factory method by the [$websocket](#websocket) service: in fact,
it lets to make different websockets that are pointing to different urls.

Example of a basic instantiation:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new('ws://localhost:12345', ['binary', 'base64']);
});
```

Using Object configuration:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        lazy: false,
        reconnect: true,
        reconnectInterval: 2000,
        enqueue: false,
        mock: false,
        protocols: ['binary', 'base64']
    });
});
```

Following the explanation of the configuration object - {Type} PropertyName (default):

  - **{Boolean} lazy (false)**: lazy initialization. A websocket can open the connection when ngWebsocket is instantiated with [$websocket.$new](#new) (false) or afterwards with [$open](#open) (false). For more information see [Features - Lazy Initialization](#lazy)
  - **{Boolean} reconnect (true)**: auto reconnect behaviour. A websocket can try to reopen the connection when is down (true) or stay closed (false). For more information see [Features - Auto Reconnect](#reconnect)
  - **{Number} reconnectInterval (2000)**: auto reconnect interval. By default, a websocket try to reconnect after 2000 ms (2 seconds). For more information see [Features - Auto Reconnect](#reconnect)
  - **{Boolean} enqueue (false)**: enqueue unsent messages. By default, a websocket discards messages when the connection is closed (false) but it can enqueue them and send afterwards the connection gets open back (true). For more information see [Features - Enqueue Unsent Messages](#enqueue)
  - **{Boolean/Object} mock (false)**: mock a websocket server. By default, a websocket run only if the webserver socket is listening (false) but it can be useful to mock the backend to make the websocket working (true). For more information see [Features - Mock Websocket Server](#mock)
  - **{String/String[]} (null)**: Either a single protocol string or an array of protocol strings. This is the same as the WebSocket protocols argument.

### Constants

Websocket status constants:

  - **$CONNECTING**: the websocket is trying to open the connection
  - **$OPEN**: the websocket connection is open
  - **$CLOSING**: the websocket connection is closing
  - **$CLOSED**: the websocket connection is closed

### Events

There are custom events fired by ngWebsocket.
They are useful to setup a listener for certain situations and behaviours:

  - **$open**: the websocket gets open
  - **$close**: the websocket gets closed
  - **$error**: an error occurred (callback params: {Error} error)
  - **$message**: the original message sent from the server (callback params: {String} message). Usually, it's a JSON encoded string containing the event to fire and the data to pass ({"event": "an event", "data": "some data"})

The other events are custom events, setup by the user itself.

### $on

Attach one or more handlers to a specific event.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new('ws://localhost:12345');

    // Single event handler
    ws.$on('my event', function myHandler () {...});

    // Different event handlers
    ws.$on('another event', myHandler, mySecondHandler, myThirdHandler);

    // Different chained event handlers
    ws.$on('third event', function myHandler () {...})
      .$on('third event', function mySecondHandler () {...})
      .$on('third event', function myThirdHandler () {...});
});
```

Now the websocket is listening for 'my event' event and the handler 'myHandler' will be called when that event
is sent by the websocket server. The same thing happens for the other two cases: each event handler is called
one by one, starting from the first one, ending with the last one.

**Usage**

```javascript
$on(event, handler|handlers)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| event | String | the event to attach a listener |
| handler/handlers | Function/Function[] | one or more handlers to invoke when the event is fired up |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | the ngWebsocket |

### $un

Detach a handler from a specific event.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new('ws://localhost:12345');

    ws.$on('my event', function myHandler () {...});
    ws.$un('my event');
});
```

The above websocket has not listener attached at the end of the execution.


**Usage**

```javascript
$un(event)
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| event     | String   | the event to detach the listener |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | the ngWebsocket |

### $emit

Send an event to the websocket server.

It's possible to send a lonely event or attaching some data to it.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new('ws://localhost:12345');

    ws.$on('$open', function () {
        ws.$emit('lonely event'); // the websocket server will receive only the event name
        ws.$emit('event with data', 'some data'); // it will send the event with 'some data' string
        ws.$emit('with object', {some: 'data'}); // it will send the event with the object JSONified
    });
});
```

It's possible to send both simply (like strings and numbers) and complex data (like objects and arrays).

**Usage**

```javascript
$emit(event, [data])
```

**Arguments**

| **Param** | **Type** | **Details** |
| --------- | -------- | ----------- |
| event     | String   | the event to send |
| data (optional) | String/Number/Object | the data to send with the event |

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | the ngWebsocket |

### $open

Open the websocket connection if it's closed.

```javascript
angular.run(function ($websocket, $timeout) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        lazy: true
    });

    ws.$on('$open', function () {
        console.log('The websocket now is open');
    });

    $timeout(function () {
        ws.$open(); // it will open the websocket after 5 seconds
    }, 5000);
```

**Usage**

```javascript
$open()
```

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | the ngWebsocket |

### $close

It closes the websocket connection if it's open.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new(url: 'ws://localhost:12345');

    ws.$on('$open', function () {
        ws.$close(); // it closes the websocket connection
    });

    ws.$on('$close', function () {
        console.log('Connection closed!');
    });
```

**Usage**

```javascript
$close()
```

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| ngWebsocket | the ngWebsocket |

### $status

It returns the current status of the websocket connection.
It's possible to use the [websocket constants](#constants) to make checks.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new(url: 'ws://localhost:12345');

    console.log(ws.$status()); // it prints ws.$CONNECTING

    ws.$on('$open', function () {
        console.log(ws.$status()); // it prints ws.$OPEN
        ws.$close(); // it closes the websocket connection
        console.log(ws.$status()); // it prints ws.$CLOSING
    });

    ws.$on('$close', function () {
        console.log(ws.$status()); // it prints ws.$CLOSED
        console.log('Connection closed!');
    });
```

**Usage**

```javascript
$status()
```

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| Number   | a constant number representing the websocket connection readyState |

### $ready

It returns if the websocket connection is open or closed.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new(url: 'ws://localhost:12345');

    console.log(ws.$ready()); // it prints false

    ws.$on('$open', function () {
        console.log(ws.$ready()); // it prints true
        ws.$close(); // it closes the websocket connection
        console.log(ws.$ready()); // it prints false
    });

    ws.$on('$close', function () {
        console.log(ws.$ready()); // it prints false
        console.log('Connection closed!');
    });
```

**Usage**

```javascript
$ready()
```

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| Boolean  | true if the connection is OPEN, false otherwise |

### $mockup

It returns if the websocket is mocked up or not.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new(url: 'ws://localhost:12345');

    console.log(ws.$mockup()); // it prints false

    var ws2 = $websocket.$new({
        url: 'ws://localhost:54321',
        mock: true
    });

    console.log(ws.$mockup()); // it prints true
```

**Usage**

```javascript
$mockup()
```

**Returns**

| **Type** | **Details** |
| -------- | ----------- |
| Boolean  | true if the ngWebsocket istance is mocked up, false otherwise |

## $$mockWebsocket

If you need to develop or test your application without a real websocket backend server, you can setup
a mockup of it with this feature.
The only thing to do is to pass a configuration object during the ngWebsocket initialization:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        mock: {
            openTimeout: 500,
            closeTimeout: 1000,
            messageInterval: 2000,
            fixtures: {}
        }
    });
```

Following the explanation of the configuration object - {Type} PropertyName (default)::

  - **{Boolean/Object} mock (false)**: could be either a Boolean (default to false) or an object
  - **{Number} openTimeout (500)**: timeout to make the internal websocket to get open
  - **{Number} closeTimeout (1000)**: timeout to make the internal websocket to get closed
  - **{Number} messageInterval (2000)**: the internal websocket sends enqueued message with this interval time
  - **{Object/String} fixtures ({})**: an object of fixtures, where the keys are the events and the values are the data to respond, or an url to retrieve remote fixtures via HTTP

Fixtures can mock both custom events and data.
They can be added as a static object with the following structure:

```javascript
fixtures: {
    'incoming event name': {
        event: 'outgoing event name',
        data: 'response data'
    }
}
```

The *incoming event name* is the event fired by the websocket while the *outgoing event name* is the one sent by the mocked webserver.
So, it be useful to map events with a custom response.
By default, the mock feature acts like a parrot server, responding with the same data on the same received event.

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
        url: 'ws://localhost:12345',
        mock: {
            fixtures: {
                'mock data': {
                    data: {
                        hello: 'world'
                    }
                },
                'mock data and event': {
                    event: 'custom event',
                    data: {
                        hello: 'mocked world'
                    }
                }
            }
        }
    });

    ws.$on('$open', function () {
        ws.$emit('parrot event', 'parrot data')
          .$emit('mock data')
          .$emit('mock data and event');
      })
      .$on('parrot event', function (message) {
        console.log(message); // it prints 'parrot data'
      })
      .$on('mock data', function (message) {
        console.log(message); // it prints '{hello: 'world'}'
      })
      .$on('custom event', function (message) {
        console.log(message); // it prints '{hello: 'mocked world'}'
      });
```

Fixtures can be loaded through an HTTP request.
In fact, it be useful to have those in a JSON file or created by the webserver:

```javascript
angular.run(function ($websocket) {
    var ws = $websocket.$new({
            url: 'ws://localhost:12345',
            mock: {
                fixtures: '/fixtures.json' // fixtures are located in a file or calculated at run-time by the web server
            }
        });
    
    // Now you're ready to use fixtures because the websocket will be available only when the fixtures are loaded
});
```

# Contribute

Wanna contribute, fella?
That's the right place to find useful information!

How?

  - improve and fix the documentation
  - test it
  - make some demos
  - use it
  - write new pieces of code
  - optimize it
  - find bugs

And don't forget to make **pull requests**, damn it!

# License

Check out LICENSE file (MIT)