@jskit-ai/realtime 0.1.247 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 - 2026-10-29
4
+
5
+ - Translation ready: every user-visible text is a message key with its English in `locales/en.json`, so an app translates it by supplying translation files only. With no translations, texts and behaviour are unchanged. See the guide "Translating a JSKIT app".
6
+
7
+ ### New message keys (6)
8
+
9
+ All keys in `locales/en.json` (exported as `./locales/en.json`) are new in this version.
10
+
11
+ <details><summary>Key list</summary>
12
+
13
+ - `jskit.realtime.indicator.connected`
14
+ - `jskit.realtime.indicator.connectedTooltip`
15
+ - `jskit.realtime.indicator.disconnected`
16
+ - `jskit.realtime.indicator.disconnectedTooltip`
17
+ - `jskit.realtime.indicator.reconnect`
18
+ - `jskit.realtime.indicator.unavailableTooltip`
19
+
20
+ </details>
@@ -0,0 +1,14 @@
1
+ {
2
+ "jskit": {
3
+ "realtime": {
4
+ "indicator": {
5
+ "connected": "Realtime connected",
6
+ "connectedTooltip": "Realtime is connected. Live updates are active.",
7
+ "disconnected": "Realtime disconnected",
8
+ "disconnectedTooltip": "Live updates are disconnected. Click to reconnect. If your session expired, sign in again.",
9
+ "reconnect": "Reconnect live updates",
10
+ "unavailableTooltip": "Realtime is unavailable. Live updates are disabled for this page."
11
+ }
12
+ }
13
+ }
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/realtime",
3
- "version": "0.1.247",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -11,7 +11,8 @@
11
11
  "./client": "./src/client/RealtimeClientProvider.js",
12
12
  "./client/RealtimeClientProvider": "./src/client/RealtimeClientProvider.js",
13
13
  "./client/composables/useRealtimeEvent": "./src/client/composables/useRealtimeEvent.js",
14
- "./client/runtime": "./src/client/runtime.js"
14
+ "./client/runtime": "./src/client/runtime.js",
15
+ "./locales/en.json": "./locales/en.json"
15
16
  },
16
17
  "dependencies": {
17
18
  "@socket.io/redis-adapter": "^8.3.0",
@@ -20,7 +21,7 @@
20
21
  "socket.io-client": "^4.8.3"
21
22
  },
22
23
  "peerDependencies": {
23
- "@jskit-ai/kernel": "0.1.247",
24
+ "@jskit-ai/kernel": "0.2.0",
24
25
  "vue": "^3.5.13"
25
26
  },
26
27
  "description": "Thin, generic realtime runtime wrappers for socket.io server and client.",
@@ -90,7 +91,11 @@
90
91
  },
91
92
  {
92
93
  "subpath": "./client/runtime",
93
- "summary": "Exports createSocketIoClient and disconnectSocketIoClient."
94
+ "summary": "Exports createSocketIoClient, disconnectSocketIoClient and reconnectSocketIoClient."
95
+ },
96
+ {
97
+ "subpath": "./locales/en.json",
98
+ "summary": "English catalog for the realtime connection indicator, registered by the generated client bootstrap."
94
99
  }
95
100
  ],
96
101
  "capabilities": {
@@ -2,7 +2,7 @@
2
2
  "private": true,
3
3
  "type": "module",
4
4
  "dependencies": {
5
- "@jskit-ai/realtime": "0.1.247",
6
- "@jskit-ai/shell-web": "0.1.254"
5
+ "@jskit-ai/realtime": "0.2.0",
6
+ "@jskit-ai/shell-web": "0.2.0"
7
7
  }
8
8
  }
@@ -1,5 +1,9 @@
1
1
  import { computed, defineComponent, h, onBeforeUnmount, onMounted, ref } from "vue";
2
2
  import { EMPTY_REALTIME_SOCKET, useRealtimeSocket } from "../composables/useRealtimeEvent.js";
3
+ import { reconnectSocketIoClient } from "../runtime.js";
4
+ import { useJskitText } from "../i18n.js";
5
+
6
+ const { t } = useJskitText();
3
7
 
4
8
  const ROOT_STYLE = Object.freeze({
5
9
  alignItems: "center",
@@ -36,12 +40,12 @@ const SR_ONLY_STYLE = Object.freeze({
36
40
 
37
41
  function resolveTooltipText({ realtimeAvailable, connected }) {
38
42
  if (!realtimeAvailable) {
39
- return "Realtime is unavailable. Live updates are disabled for this page.";
43
+ return t("jskit.realtime.indicator.unavailableTooltip");
40
44
  }
41
45
  if (connected) {
42
- return "Realtime is connected. Live updates are active.";
46
+ return t("jskit.realtime.indicator.connectedTooltip");
43
47
  }
44
- return "Live updates are disconnected. Click to reconnect. If your session expired, sign in again.";
48
+ return t("jskit.realtime.indicator.disconnectedTooltip");
45
49
  }
46
50
 
47
51
  const RealtimeConnectionIndicator = defineComponent({
@@ -59,7 +63,9 @@ const RealtimeConnectionIndicator = defineComponent({
59
63
  });
60
64
  });
61
65
 
62
- const statusLabel = computed(() => (connected.value ? "Realtime connected" : "Realtime disconnected"));
66
+ const statusLabel = computed(() =>
67
+ connected.value ? t("jskit.realtime.indicator.connected") : t("jskit.realtime.indicator.disconnected")
68
+ );
63
69
 
64
70
  const dotStyle = computed(() => ({
65
71
  ...DOT_STYLE,
@@ -111,11 +117,13 @@ const RealtimeConnectionIndicator = defineComponent({
111
117
  "button",
112
118
  {
113
119
  disabled: !realtimeAvailable || connected.value,
114
- onClick: () => socket.connect?.(),
120
+ onClick: () => reconnectSocketIoClient(socket),
115
121
  style: { ...ROOT_STYLE, cursor: realtimeAvailable && !connected.value ? "pointer" : "default" },
116
122
  title: tooltipText.value,
117
123
  type: "button",
118
- "aria-label": realtimeAvailable && !connected.value ? "Reconnect live updates" : statusLabel.value
124
+ "aria-label": realtimeAvailable && !connected.value
125
+ ? t("jskit.realtime.indicator.reconnect")
126
+ : statusLabel.value
119
127
  },
120
128
  [
121
129
  h("span", { style: dotStyle.value, "aria-hidden": "true" }),
@@ -1,3 +1,5 @@
1
+ import { reconnectSocketIoClient } from "./runtime.js";
2
+
1
3
  function attachSocketConnectionRecovery(socket) {
2
4
  let timer = null;
3
5
  let delay = 1_000;
@@ -44,9 +46,12 @@ function attachSocketConnectionRecovery(socket) {
44
46
  }
45
47
 
46
48
  function onAvailable() {
47
- if (!canRetry()) return;
49
+ if (socket.connected || document?.hidden || window?.navigator?.onLine === false ||
50
+ !socket.io.reconnection() || (!socket.active && !recovering)) return;
51
+ // A page that wakes or comes back online reconnects now, not after the
52
+ // remaining backoff delay that was scheduled before it slept.
48
53
  clearRetry();
49
- socket.connect();
54
+ reconnectSocketIoClient(socket);
50
55
  }
51
56
 
52
57
  socket.on("connect", onConnect);
@@ -0,0 +1,8 @@
1
+ import { registerJskitMessages, useJskitText } from "@jskit-ai/kernel/client/i18n";
2
+ import en from "../../locales/en.json" with { type: "json" };
3
+
4
+ // realtime's English, registered on import so its indicator translates in
5
+ // fixtures and tests as well as in a bootstrapped app.
6
+ registerJskitMessages(en);
7
+
8
+ export { useJskitText };
@@ -31,7 +31,21 @@ function disconnectSocketIoClient(socket) {
31
31
  socket.disconnect();
32
32
  }
33
33
 
34
+ // Socket.IO ignores connect() while its own reconnection waits out a backoff
35
+ // delay or an attempt left hanging by a sleeping laptop. Closing that pending
36
+ // attempt first makes connect() open a fresh transport immediately.
37
+ function reconnectSocketIoClient(socket) {
38
+ if (!socket || socket.connected || typeof socket.connect !== "function") {
39
+ return;
40
+ }
41
+ if (socket.active) {
42
+ socket.disconnect();
43
+ }
44
+ socket.connect();
45
+ }
46
+
34
47
  export {
35
48
  createSocketIoClient,
36
- disconnectSocketIoClient
49
+ disconnectSocketIoClient,
50
+ reconnectSocketIoClient
37
51
  };
@@ -33,6 +33,11 @@ function debugEnabled(config, env) {
33
33
  return parseDebugFlag(env?.JSKIT_REALTIME_DEBUG, parseDebugFlag(config?.realtime?.debug, false));
34
34
  }
35
35
 
36
+ // The kernel's code for this English, so a client can show it in the user's language.
37
+ function authenticationRequiredError() {
38
+ return Object.assign(new Error("Authentication required."), { statusCode: 401, code: "AUTHENTICATION_REQUIRED" });
39
+ }
40
+
36
41
  function createRealtimeCapability({ io }) {
37
42
  const capability = Object.freeze({
38
43
  onConnection(listener) {
@@ -49,7 +54,7 @@ function createRealtimeCapability({ io }) {
49
54
  socket, request, authService: state.authService, audiences: state.audiences
50
55
  });
51
56
  if (!socket.connected || (!actor && realtimeAuthenticationRequired(state.authService))) {
52
- throw Object.assign(new Error("Authentication required."), { statusCode: 401 });
57
+ throw authenticationRequiredError();
53
58
  }
54
59
  if (actor) request.user = actor;
55
60
  return request;
@@ -70,7 +75,7 @@ function createRealtimeCapability({ io }) {
70
75
  readRequest,
71
76
  async authenticate() {
72
77
  const request = await readRequest();
73
- if (!request.user) throw Object.assign(new Error("Authentication required."), { statusCode: 401 });
78
+ if (!request.user) throw authenticationRequiredError();
74
79
  return request;
75
80
  }
76
81
  });
@@ -4,7 +4,7 @@ import { EventEmitter, once } from "node:events";
4
4
  import { createServer } from "node:http";
5
5
  import { Server } from "socket.io";
6
6
 
7
- import { createSocketIoClient, disconnectSocketIoClient } from "../src/client/runtime.js";
7
+ import { createSocketIoClient, disconnectSocketIoClient, reconnectSocketIoClient } from "../src/client/runtime.js";
8
8
  import { attachSocketConnectionRecovery } from "../src/client/connectionRecovery.js";
9
9
 
10
10
  test("createSocketIoClient calls connect with fixed socket path", () => {
@@ -212,3 +212,50 @@ test("a real server-forced disconnect reconnects through a fresh authenticated h
212
212
  assert.equal(handshakes, 3);
213
213
  assert.notEqual(client.id, before);
214
214
  });
215
+
216
+ test("reconnect now opens a fresh transport while Socket.IO waits out its backoff delay", async (t) => {
217
+ const http = createServer();
218
+ const io = new Server(http);
219
+ http.listen(0, "127.0.0.1");
220
+ await once(http, "listening");
221
+ const client = createSocketIoClient({
222
+ url: `http://127.0.0.1:${http.address().port}`,
223
+ options: { randomizationFactor: 0, reconnectionDelay: 60_000, reconnectionDelayMax: 60_000 }
224
+ });
225
+ t.after(async () => {
226
+ client.disconnect();
227
+ await new Promise((resolve) => io.close(resolve));
228
+ });
229
+ await once(client, "connect", { signal: AbortSignal.timeout(5_000) });
230
+ const disconnected = once(client, "disconnect");
231
+ io.sockets.sockets.get(client.id).conn.close();
232
+ assert.equal((await disconnected)[0], "transport close");
233
+ assert.equal(client.active, true);
234
+ client.connect();
235
+ await new Promise((resolve) => setTimeout(resolve, 300));
236
+ assert.equal(client.connected, false, "plain connect() waits for the 60 s backoff");
237
+ const connected = once(client, "connect", { signal: AbortSignal.timeout(5_000) });
238
+ reconnectSocketIoClient(client);
239
+ await connected;
240
+ assert.equal(client.connected, true);
241
+ });
242
+
243
+ test("a page that wakes mid-backoff reconnects now unless the app disconnected on purpose", (t) => {
244
+ const previousDocument = globalThis.document;
245
+ const document = Object.assign(new EventTarget(), { hidden: false });
246
+ globalThis.document = document;
247
+ t.after(() => {
248
+ if (previousDocument === undefined) delete globalThis.document;
249
+ else globalThis.document = previousDocument;
250
+ });
251
+ const { socket } = recoverySocket(t);
252
+ socket.disconnect = t.mock.fn(() => { socket.active = false; });
253
+ socket.active = true;
254
+ socket.emit("disconnect", "transport close");
255
+ document.dispatchEvent(new Event("visibilitychange"));
256
+ assert.equal(socket.disconnect.mock.callCount(), 1);
257
+ assert.equal(socket.connect.mock.callCount(), 1);
258
+ socket.emit("disconnect", "io client disconnect");
259
+ document.dispatchEvent(new Event("visibilitychange"));
260
+ assert.equal(socket.connect.mock.callCount(), 1);
261
+ });
@@ -0,0 +1,18 @@
1
+ import assert from "node:assert/strict";
2
+ import path from "node:path";
3
+ import test from "node:test";
4
+ import { fileURLToPath } from "node:url";
5
+ import { evaluateI18nCatalogContract } from "../../../tooling/test-support/i18nCatalogContract.mjs";
6
+
7
+ const TEST_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
8
+ const REPO_ROOT = path.resolve(TEST_DIRECTORY, "..", "..", "..");
9
+
10
+ test("realtime English catalog matches its source", () => {
11
+ const { problems } = evaluateI18nCatalogContract({
12
+ repoRoot: REPO_ROOT,
13
+ packageDir: path.join(REPO_ROOT, "packages", "realtime"),
14
+ // The client socket's injection key shares the jskit.realtime prefix but is not a message key.
15
+ nonMessageLiterals: ["jskit.realtime.runtime.client.socket"]
16
+ });
17
+ assert.deepEqual(problems, []);
18
+ });
@@ -0,0 +1,41 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { createSSRApp, h } from "vue";
4
+ import { renderToString } from "vue/server-renderer";
5
+ import { createI18n } from "vue-i18n";
6
+ import { setJskitI18n } from "@jskit-ai/kernel/client/i18n";
7
+ import { RealtimeConnectionIndicator } from "../src/client/components/RealtimeConnectionIndicator.js";
8
+
9
+ function renderIndicator() {
10
+ return renderToString(createSSRApp({ render: () => h(RealtimeConnectionIndicator) }));
11
+ }
12
+
13
+ test("realtime indicator texts are English by default and follow the app's language", async () => {
14
+ const english = await renderIndicator();
15
+ assert.match(english, /title="Realtime is unavailable\. Live updates are disabled for this page\."/u);
16
+ assert.match(english, />Realtime disconnected</u);
17
+
18
+ setJskitI18n(createI18n({
19
+ legacy: false,
20
+ locale: "xx",
21
+ messages: {
22
+ xx: {
23
+ jskit: {
24
+ realtime: {
25
+ indicator: {
26
+ unavailableTooltip: "[xx] Temps réel indisponible.",
27
+ disconnected: "[xx] Déconnecté"
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }));
34
+ try {
35
+ const translated = await renderIndicator();
36
+ assert.match(translated, /title="\[xx\] Temps réel indisponible\."/u);
37
+ assert.match(translated, />\[xx\] Déconnecté</u);
38
+ } finally {
39
+ setJskitI18n(null);
40
+ }
41
+ });