Commit Graph

1651 Commits

Author SHA1 Message Date
KC Erb
6abfa1fa4c feat: add autoUnref option
With autoUnref set to true (default: false), the Socket.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

```js
const socket = io({
  autoUnref: true
});
```

Note: this option only applies to Node.js clients.

Related: https://github.com/socketio/socket.io-client/issues/1446
2021-03-10 12:16:12 +01:00
Damien Arrachequesne
59023657a0 feat: add support for typed events
Syntax:

```ts
interface ServerToClientEvents {
  "my-event": (a: number, b: string, c: number[]) => void;
}

interface ClientToServerEvents {
  hello: (message: string) => void;
}

const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io();

socket.emit("hello", "world");

socket.on("my-event", (a, b, c) => {
  // ...
});
```

The events are not typed by default (inferred as any), so this change
is backward compatible.

Related: https://github.com/socketio/socket.io/issues/3742
2021-03-10 01:24:56 +01:00
Damien Arrachequesne
78ec5a6e43 chore(release): 3.1.2
Diff: https://github.com/socketio/socket.io-client/compare/3.1.1...3.1.2
2021-02-26 01:10:30 +01:00
Damien Arrachequesne
83a65be78a chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/4.1.0...4.1.2
2021-02-26 01:08:39 +01:00
Damien Arrachequesne
13b32b39a4 fix: restore support for web workers
A previous change ([1], included in 3.0.5) broke support for web
workers, which threw "window is not defined" when importing the bundle
in a web worker.

Related: https://github.com/socketio/socket.io/issues/3809

Reference: https://webpack.js.org/configuration/output/#outputglobalobject

[1]: 8c08c5d5c3
2021-02-26 00:43:26 +01:00
Damien Arrachequesne
311c5d14c3 chore(release): 3.1.1
Diff: https://github.com/socketio/socket.io-client/compare/3.1.0...3.1.1
2021-02-03 22:36:17 +01:00
Damien Arrachequesne
7a0c2b504f fix: include the path in the manager ID
Previously, the following code:

```js
const socket1 = io({
  path: "/test1"
});
const socket2 = io({
  path: "/test2"
});
```

would result in one single Manager, with the "/test2" path being
silently ignored.

Two distinct Manager instances will now be created.

Related: https://github.com/socketio/socket.io-client/issues/1225
2021-02-03 22:31:01 +01:00
Damien Arrachequesne
61afc5d8cb fix: remove polyfill for process in the bundle
A polyfill for Node.js "process" was included in the final bundle.

Reference: https://webpack.js.org/configuration/node/
2021-02-02 12:08:26 +01:00
david-fong
47f917afdd fix(typings): add return types and general-case overload signatures (#1440) 2021-02-02 11:29:08 +01:00
Mick Lawitzke
f02ab3bc96 fix(typings): fix the type of the "query" option (#1439)
Having type `Object` it was not possible to set values, e.g.:

```ts
if (!this.socket.io.opts.query) {
  this.socket.io.opts.query = {};
}
this.socket.io.opts.query.token = 'abc123';
```

Results in error:

> Element implicitly has an 'any' type because expression of type '"token"' can't be used to index type 'Object'.
2021-02-02 11:26:33 +01:00
Damien Arrachequesne
be81a2ce6f chore: point towards the master branch for the CI badge 2021-01-28 14:17:28 +01:00
Damien Arrachequesne
0a63a17f63 refactor: remove unused line
In Socket.IO v2, the Socket `query` option was sent when connecting to
a custom namespace (but not for the main namespace).

This was fixed in Socket.IO v3 ([1]), so this line is now useless.

[1]: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/#Add-a-clear-distinction-between-the-Manager-query-option-and-the-Socket-query-option
2021-01-28 12:27:02 +01:00
Damien Arrachequesne
5529f34aaf chore(release): 3.1.0
Diff: https://github.com/socketio/socket.io-client/compare/3.0.5...3.1.0
2021-01-15 02:15:57 +01:00
Damien Arrachequesne
5d9b4eb42b chore: bump socket.io-parser version
Diff: https://github.com/socketio/socket.io-parser/compare/4.0.3...4.0.4
2021-01-15 01:59:56 +01:00
Damien Arrachequesne
13e16b9b79 chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/4.0.6...4.1.0
2021-01-15 01:56:01 +01:00
Mick Lawitzke
fe97243fab fix(typings): make Manager#opts public (#1437)
As stated in the [documentation][1], the query option can be set by
setting `socket.io.opts` but `opts` was private and readonly.

[1]: https://socket.io/docs/v3/client-api/#With-query-option
2021-01-11 22:47:28 +01:00
Damien Arrachequesne
4922e397f4 docs: points towards the website
The website is now much more stable, so there's no need to keep two
copies of the same content (which must be manually kept in sync).
2021-01-08 15:13:43 +01:00
Damien Arrachequesne
bcdd3bef85 chore(release): 3.0.5
Diff: https://github.com/socketio/socket.io-client/compare/3.0.4...3.0.5
2021-01-05 12:02:52 +01:00
Damien Arrachequesne
cf9fc35836 chore: bump debug version 2021-01-05 11:46:06 +01:00
Damien Arrachequesne
53c73749a8 fix: emit a connect_error event upon connection failure
Related: https://github.com/socketio/socket.io/issues/3734
2021-01-05 11:42:54 +01:00
Damien Arrachequesne
b83f89c901 fix(typings): make sendBuffer and receiveBuffer public
See also: https://socket.io/docs/v3/client-offline-behavior/#Buffered-events
2021-01-05 10:17:58 +01:00
Damien Arrachequesne
8c08c5d5c3 refactor: remove global polyfill from webpack config
Fixes https://github.com/socketio/socket.io-client/issues/1407
2021-01-05 10:15:27 +01:00
Damien Arrachequesne
2874d5f972 chore: update GitHub issue templates 2020-12-30 11:45:17 +01:00
david-fong
7bc7a7b624 refactor: switch to native javascript bind (#1423)
The polyfill for the bind() method was necessary for IE6/7/8, which we
do not support anymore.

Related: https://caniuse.com/mdn-javascript_builtins_function_bind
2020-12-11 11:55:57 +01:00
Damien Arrachequesne
085cd6c56d chore(release): 3.0.4
Diff: https://github.com/socketio/socket.io-client/compare/3.0.3...3.0.4
2020-12-07 11:43:11 +01:00
Damien Arrachequesne
f8f60fc860 fix: keep track of active sockets
When a given socket was disconnected, either by the server-side or by the client-side, the manager was closed too, regardless of the other connected sockets.

```js
const socket1 = io({
  autoConnect: false
});
const socket2 = io("/test");

socket1.disconnect(); // also disconnect socket2
```

This bug was introduced in [1].

[1]: b60e909039
2020-12-07 11:24:15 +01:00
Damien Arrachequesne
ec1f8c3474 fix: emit an error when reaching a v2.x server
A WebSocket-only v3.x client is able to reach a v2.x server, as the
Engine.IO handshake are compatible, but the v2.x server does not send
a sid in the Socket.IO handshake, which resulted in:

> Uncaught TypeError: Cannot read property 'sid' of undefined

A 'connect_error' event will now be emitted.

References:

- https://github.com/socketio/engine.io-protocol#difference-between-v3-and-v4
- https://github.com/socketio/socket.io-protocol#difference-between-v5-and-v4
- https://socket.io/docs/v3/migrating-from-2-x-to-3-0/
2020-12-07 11:18:55 +01:00
david-fong
226b491846 refactor(typings): make typing information more specific (#1418) 2020-12-07 09:06:57 +01:00
Damien Arrachequesne
adffc03977 ci: migrate to GitHub Actions
Due to the recent changes to the Travis CI platform (see [1]), we will
now use GitHub Actions to run the tests.

Reference: https://docs.github.com/en/free-pro-team@latest/actions/guides/building-and-testing-nodejs

[1]: https://blog.travis-ci.com/2020-11-02-travis-ci-new-billing
2020-12-07 09:02:41 +01:00
Luiz Pereira
b3de861a92 fix(typings): export extraHeaders option (#1410) 2020-11-20 23:09:27 +01:00
Damien Arrachequesne
66e4fdf2e6 chore(release): 3.0.3
Diff: https://github.com/socketio/socket.io-client/compare/3.0.2...3.0.3
2020-11-19 01:01:33 +01:00
Damien Arrachequesne
e2a4c56709 chore: cleanup dist folder before compilation 2020-11-19 00:59:42 +01:00
Damien Arrachequesne
bec15240ea fix: properly export io in ES modules wrapper
The syntax was invalid:

```
export const io = io;
             ^

SyntaxError: Identifier 'io' has already been declared
    at Loader.moduleStrategy (internal/modules/esm/translators.js:122:18)
```
2020-11-19 00:56:25 +01:00
Damien Arrachequesne
a883e3238e chore(release): 3.0.2
Diff: https://github.com/socketio/socket.io-client/compare/3.0.1...3.0.2
2020-11-18 00:00:42 +01:00
Damien Arrachequesne
719307801a fix(typings): export withCredentials option
Related: https://github.com/socketio/socket.io-client/issues/1405
2020-11-17 23:54:57 +01:00
Damien Arrachequesne
625d146f28 chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/4.0.2...4.0.4
2020-11-17 23:54:57 +01:00
Damien Arrachequesne
7b3ec9fad9 fix: add io as named exports
The TypeScript (build/index.d.ts) and ES modules exports did not match,
which caused issues with webpack and ts-loader.

Related: https://github.com/socketio/socket.io-client/issues/1396
2020-11-17 23:54:56 +01:00
Yusuke Sakurai
96cd2c9ae4 fix(typings): export ManagerOptions (#1398) 2020-11-17 00:03:22 +01:00
Damien Arrachequesne
969d5c95f8 chore(release): 3.0.1
Diff: https://github.com/socketio/socket.io-client/compare/3.0.0...3.0.1
2020-11-09 10:27:33 +01:00
Damien Arrachequesne
ca34eddf4b chore: bump engine.io-client version 2020-11-09 10:23:42 +01:00
Avi Vahl
19ab1e9e4e fix(typings): export Socket and SocketOptions types (#1394)
These were exposed by @types/socket.io-client.
2020-11-09 09:00:07 +01:00
Damien Arrachequesne
3add3e1b82 chore: restore package-lock.json file 2020-11-05 22:27:32 +01:00
Damien Arrachequesne
b7e07ba633 chore(release): 3.0.0
Diff: https://github.com/socketio/socket.io-client/compare/2.3.1...3.0.0
2020-11-05 22:06:25 +01:00
Damien Arrachequesne
ffa280450d chore(release): 3.0.0-rc4
Diff: https://github.com/socketio/socket.io-client/compare/3.0.0-rc3...3.0.0-rc4
2020-10-30 23:02:06 +01:00
Damien Arrachequesne
09393952e3 feat: emit an Error object upon middleware error
See 54bf4a44e9
2020-10-30 22:54:13 +01:00
Damien Arrachequesne
969debe88c refactor: rework of the Manager events
- rename "connect_error" to "error"
- remove "reconnecting" (duplicate of "reconnect_attempt")

The updated list of events emitted by the Manager:

- open:                successful (re)connection
- error:               (re)connection failure (previously: "connect_error") or error after a successful connection
- close:               disconnection

- ping:                ping packet
- packet:              data packet

- reconnect_attempt:   reconnection attempt (previously: "reconnect_attempt" & "reconnecting")
- reconnect:           successful reconnection
- reconnect_error:     reconnection failure
- reconnect_failed:    reconnection failure after all attempts

For reference, the Socket instance emits the following events:

- connect:             successful connection to a Namespace
- connect_error:       connection failure
- disconnect:          disconnection
2020-10-27 23:07:44 +01:00
Damien Arrachequesne
a9127cee83 chore(release): 3.0.0-rc3
Diff: https://github.com/socketio/socket.io-client/compare/3.0.0-rc2...3.0.0-rc3
2020-10-27 00:41:32 +01:00
Damien Arrachequesne
13e1db7c94 refactor: rename ERROR to CONNECT_ERROR
The meaning is not modified: this packet type is still used by the
server when the connection to a namespace is refused.

Breaking change: the Socket instance will now emit a "connect_error"
event instead of "error" (which is not a reserved event anymore)

```js
// before
socket.on("error", () => {});

// after
socket.on("connect_error", () => {});
```
2020-10-26 10:04:05 +01:00
Damien Arrachequesne
55f464f59e feat: add support for catch-all listeners
Inspired from EventEmitter2 [1]

The API is similar to the one on the server-side:

```js
socket.onAny((event, ...args) => {});

socket.prependAny((event, ...args) => {});

socket.offAny(); // remove all listeners

socket.offAny(listener);

const listeners = socket.listenersAny();
```

[1]: https://github.com/EventEmitter2/EventEmitter2
2020-10-26 10:03:27 +01:00
Damien Arrachequesne
71d60480af feat: add bundle with msgpack parser
Pros:

- events with binary content are sent as 1 WebSocket frame (instead of 2 with the default parser)
- payloads with lots of numbers should be smaller

Cons:

- no IE9 support (https://caniuse.com/mdn-javascript_builtins_arraybuffer)
- a slightly bigger bundle size (61.1 vs 59.9 KB)

Source: https://github.com/darrachequesne/socket.io-msgpack-parser
2020-10-25 22:47:31 +01:00