Skip to content

Commit c58ecfc

Browse files
[docs] Add code examples for registered events (#1139)
1 parent e9ebe36 commit c58ecfc

File tree

1 file changed

+120
-6
lines changed

1 file changed

+120
-6
lines changed

docs/API.md

+120-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,27 @@
5151
- [Event: 'reconnecting'](#event-reconnecting-1)
5252
- [Event: 'reconnect_error'](#event-reconnect_error-1)
5353
- [Event: 'reconnect_failed'](#event-reconnect_failed-1)
54+
- [Event: 'ping'](#event-ping-1)
55+
- [Event: 'pong'](#event-pong-1)
5456

5557

5658
### IO
5759

5860
Exposed as the `io` namespace in the standalone build, or the result of calling `require('socket.io-client')`.
5961

62+
```html
63+
<script src="/socket.io/socket.io.js"></script>
64+
<script>
65+
const socket = io('http://localhost');
66+
</script>
67+
```
68+
69+
```js
70+
const io = require('socket.io-client');
71+
// or with import syntax
72+
import io from 'socket.io-client';
73+
```
74+
6075
#### io.protocol
6176

6277
* _(Number)_
@@ -372,11 +387,11 @@ Fired when a pong is received from the server.
372387
An unique identifier for the socket session. Set after the `connect` event is triggered, and updated after the `reconnect` event.
373388

374389
```js
375-
var socket = io('http://localhost');
390+
const socket = io('http://localhost');
376391

377392
console.log(socket.id); // undefined
378393

379-
socket.on('connect', function(){
394+
socket.on('connect', () => {
380395
console.log(socket.id); // 'G5p5...'
381396
});
382397
```
@@ -433,13 +448,13 @@ socket.emit('with-binary', 1, '2', { 3: '4', 5: new Buffer(6) });
433448
The `ack` argument is optional and will be called with the server answer.
434449

435450
```js
436-
socket.emit('ferret', 'tobi', function (data) {
451+
socket.emit('ferret', 'tobi', (data) => {
437452
console.log(data); // data will be 'woot'
438453
});
439454

440455
// server:
441-
// io.on('connection', function (socket) {
442-
// socket.on('ferret', function (name, fn) {
456+
// io.on('connection', (socket) => {
457+
// socket.on('ferret', (name, fn) => {
443458
// fn('woot');
444459
// });
445460
// });
@@ -454,9 +469,18 @@ socket.emit('ferret', 'tobi', function (data) {
454469
Register a new handler for the given event.
455470

456471
```js
457-
socket.on('news', function (data) {
472+
socket.on('news', (data) => {
458473
console.log(data);
459474
});
475+
476+
// with multiple arguments
477+
socket.on('news', (arg1, arg2, arg3, arg4) => {
478+
// ...
479+
});
480+
// with callback
481+
socket.on('news', (cb) => {
482+
cb(0);
483+
});
460484
```
461485

462486
The socket actually inherits every method of the [Emitter](https://github.com/component/emitter) class, like `hasListeners`, `once` or `off` (to remove an event listener).
@@ -486,50 +510,140 @@ Synonym of [socket.close()](#socketclose).
486510

487511
Fired upon a connection including a successful reconnection.
488512

513+
```js
514+
socket.on('connect', () => {
515+
// ...
516+
});
517+
518+
// note: you should register event handlers outside of connect,
519+
// so they are not registered again on reconnection
520+
socket.on('myevent', () => {
521+
// ...
522+
});
523+
```
524+
489525
#### Event: 'connect_error'
490526

491527
- `error` _(Object)_ error object
492528

493529
Fired upon a connection error.
494530

531+
```js
532+
socket.on('connect_error', (error) => {
533+
// ...
534+
});
535+
```
536+
495537
#### Event: 'connect_timeout'
496538

497539
Fired upon a connection timeout.
498540

541+
```js
542+
socket.on('connect_timeout', (timeout) => {
543+
// ...
544+
});
545+
```
546+
499547
#### Event: 'error'
500548

501549
- `error` _(Object)_ error object
502550

503551
Fired when an error occurs.
504552

553+
```js
554+
socket.on('error', (error) => {
555+
// ...
556+
});
557+
```
558+
505559
#### Event: 'disconnect'
506560

561+
- `reason` _(String)_ either 'io server disconnect' or 'io client disconnect'
562+
507563
Fired upon a disconnection.
508564

565+
```js
566+
socket.on('disconnect', (reason) => {
567+
// ...
568+
});
569+
```
570+
509571
#### Event: 'reconnect'
510572

511573
- `attempt` _(Number)_ reconnection attempt number
512574

513575
Fired upon a successful reconnection.
514576

577+
```js
578+
socket.on('reconnect', (attemptNumber) => {
579+
// ...
580+
});
581+
```
582+
515583
#### Event: 'reconnect_attempt'
516584

517585
- `attempt` _(Number)_ reconnection attempt number
518586

519587
Fired upon an attempt to reconnect.
520588

589+
```js
590+
socket.on('reconnect_attempt', (attemptNumber) => {
591+
// ...
592+
});
593+
```
594+
521595
#### Event: 'reconnecting'
522596

523597
- `attempt` _(Number)_ reconnection attempt number
524598

525599
Fired upon an attempt to reconnect.
526600

601+
```js
602+
socket.on('reconnecting', (attemptNumber) => {
603+
// ...
604+
});
605+
```
606+
527607
#### Event: 'reconnect_error'
528608

529609
- `error` _(Object)_ error object
530610

531611
Fired upon a reconnection attempt error.
532612

613+
```js
614+
socket.on('reconnect_error', (error) => {
615+
// ...
616+
});
617+
```
618+
533619
#### Event: 'reconnect_failed'
534620

535621
Fired when couldn't reconnect within `reconnectionAttempts`.
622+
623+
```js
624+
socket.on('reconnect_failed', () => {
625+
// ...
626+
});
627+
```
628+
629+
#### Event: 'ping'
630+
631+
Fired when a ping packet is written out to the server.
632+
633+
```js
634+
socket.on('ping', () => {
635+
// ...
636+
});
637+
```
638+
639+
#### Event: 'pong'
640+
641+
- `ms` _(Number)_ number of ms elapsed since `ping` packet (i.e.: latency).
642+
643+
Fired when a pong is received from the server.
644+
645+
```js
646+
socket.on('pong', (latency) => {
647+
// ...
648+
});
649+
```

0 commit comments

Comments
 (0)