@arttecs/murmur 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +318 -2
- package/dist/channel.d.ts +22 -0
- package/dist/chunk-QZPDZV62.js +1086 -0
- package/dist/client.d.ts +206 -0
- package/dist/echo.d.ts +119 -0
- package/dist/echo.js +346 -0
- package/dist/envelope.d.ts +9 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/invoke-listener.d.ts +2 -0
- package/dist/stream.d.ts +34 -0
- package/dist/transport.d.ts +84 -0
- package/package.json +56 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Arttecs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,319 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Murmur JavaScript client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Install the ESM-only package with npm.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @arttecs/murmur
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Channels
|
|
10
|
+
|
|
11
|
+
Create a client with your Murmur server's SSE endpoint. Public and private channels share one connection.
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Murmur } from '@arttecs/murmur'
|
|
15
|
+
|
|
16
|
+
const murmur = new Murmur({ url: 'https://events.example.com/sse' })
|
|
17
|
+
|
|
18
|
+
const news = murmur.channel('news')
|
|
19
|
+
news.listen('.updated', data => console.log(data))
|
|
20
|
+
|
|
21
|
+
murmur.private('orders.42')
|
|
22
|
+
.listen('OrderShipped', data => console.log(data))
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`channel('news')` subscribes to `news` and `private('orders.42')` subscribes to `private-orders.42`. Each callback receives the event payload for its channel.
|
|
26
|
+
|
|
27
|
+
Joining or leaving a channel rebuilds the shared connection with the current topics and replay cursor. On attempts with a cursor, the client sends a pending topic as `fresh` until the server acknowledges it with `murmur.attached`. A fresh topic starts strictly after the sampled log tail. After attachment, the topic participates in ordinary replay and can receive retained events from an outage. Pending joins are tracked by wire topic, so leaving one alias preserves attachment while another alias remains joined.
|
|
28
|
+
|
|
29
|
+
A cursorless connection skips replay of stored history. Before the attach marker, it can receive events the server dispatched while sampling the log tail. With Redis, that can include events published shortly before the request. Wait for `attached` before fetching initial state. Matching events published after you receive the marker are delivered while the subscription remains active, subject to sender exclusion and any reported history loss.
|
|
30
|
+
|
|
31
|
+
Each retry opens a new EventSource with the current cursor, credentials, and pending joins. If the stream ends before the attach marker, the retry uses the client's backoff and the unacknowledged joins remain pending. A numbered attach marker or a numbered heartbeat after attachment replaces the saved cursor even when its ID is lower. Heartbeats stay out of application callbacks and event counts.
|
|
32
|
+
|
|
33
|
+
The default namespace is `App.Events`. The client resolves `OrderShipped` to `App\Events\OrderShipped`, converting namespace dots to backslashes. Set `namespace` to use a different namespace. Prefix an event name with a dot to use the literal name. For example, `.updated` listens for `updated`.
|
|
34
|
+
|
|
35
|
+
Laravel Echo converts dots in relative class names such as `Sub.Event` to namespace separators. Murmur applies the configured namespace prefix and preserves those dots. To listen for a class in the `Sub` sub-namespace, pass `'Sub\\Event'` in JavaScript.
|
|
36
|
+
|
|
37
|
+
Use `murmur.on(event, callback)` to listen for these client events. It returns a function that removes the listener.
|
|
38
|
+
|
|
39
|
+
- `connected` fires when the shared EventSource opens.
|
|
40
|
+
- `attached` fires each time the server confirms attachment, after tail sampling and any replay on that connection. Register the listener before joining channels if you need the first notification.
|
|
41
|
+
- `disconnected` fires when a shared transport or authorization attempt fails and enters backoff. Leaving the last channel, having every topic denied, exceeding the URL budget, and closing the client do not fire this event.
|
|
42
|
+
- `unauthorized` fires as `(status, channelName)` for each denied private channel. The name is the one passed to `murmur.private()`. Register `murmur.private(name).error(callback)` to receive the numeric status for just that channel.
|
|
43
|
+
- `error` receives an `Error` if the shared subscription URL exceeds the request budget or the server announces an unexpected protocol. The first protocol mismatch is reported once and the client continues.
|
|
44
|
+
- `gap` fires when the server reports a gap in replay. Refresh any state that depends on the missing events.
|
|
45
|
+
|
|
46
|
+
The first retry uses `retryMs` as its base delay. For consecutive failures after the first, the base delay doubles up to `maxRetryMs`. Each retry adds random jitter of up to `retryJitter` times its base delay. The attach marker resets the retry sequence. An EventSource that opens and then ends before attachment counts as a failed attempt. Native EventSource handles SSE `retry:` hints internally, so Murmur's replacement connections use the configured retry settings.
|
|
47
|
+
|
|
48
|
+
`news.stopListening('.updated')` removes the callbacks for that event. `murmur.leave('orders.42')` leaves channels joined under that name. `murmur.close()` closes the channel connection and all streams created by the client, and cancels their retries.
|
|
49
|
+
|
|
50
|
+
## Sender exclusion
|
|
51
|
+
|
|
52
|
+
`murmur.socketId()` returns the shared connection ID announced by Murmur. It returns `undefined` until that first announcement, which follows the EventSource open event. The client reuses the ID across reconnects.
|
|
53
|
+
|
|
54
|
+
Send the ID as `X-Socket-ID` on Laravel requests that broadcast with `toOthers()`. Once the ID is available, use either of these forms.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
fetch('/orders/42/ship', { method: 'POST', headers: { 'X-Socket-ID': murmur.socketId() ?? '' } })
|
|
58
|
+
axios.post('/orders/42/ship', {}, { headers: { 'X-Socket-ID': murmur.socketId() ?? '' } })
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Options
|
|
62
|
+
|
|
63
|
+
Pass these options to `new Murmur(options)`.
|
|
64
|
+
|
|
65
|
+
| Field | Type | Default | Meaning |
|
|
66
|
+
| --- | --- | --- | --- |
|
|
67
|
+
| `url` | `string` | Required | SSE endpoint for the shared channel connection. |
|
|
68
|
+
| `authEndpoint` | `string` | `'/broadcasting/auth'` | Endpoint used by the built-in authorization request. |
|
|
69
|
+
| `auth` | `AuthOptions` | `undefined` | Headers and credentials for the built-in authorization request. |
|
|
70
|
+
| `authorizer` | `Authorizer` | `undefined` | Function that issues one or more tokens for private topics in place of the built-in request. |
|
|
71
|
+
| `namespace` | `string` | `'App.Events'` | Dotted namespace for unqualified channel event names. |
|
|
72
|
+
| `retryMs` | `number` | `3000` | Base delay before the first retry, in milliseconds. Must be finite and greater than zero. |
|
|
73
|
+
| `maxRetryMs` | `number` | `30000`, or `retryMs` when that is larger | Cap on the base retry delay before jitter, in milliseconds. Must be finite and at least `retryMs`. |
|
|
74
|
+
| `retryJitter` | `number` | `0.25` | Maximum fraction of the base delay added as random jitter on each retry. Must be finite and between 0 and 1, inclusive. Set to `0` to disable jitter. |
|
|
75
|
+
| `tokenRefreshMarginSeconds` | `number` | `30` | Remaining lifetime a cached token must exceed to be reused, in seconds. Must be finite and non-negative. |
|
|
76
|
+
| `streamMaxFailures` | `number` | `8` | Consecutive attempts without attachment or replay progress before an independent stream expires. Must be an integer of at least 1. |
|
|
77
|
+
| `fetch` | `typeof fetch` | `globalThis.fetch` | Fetch implementation for the built-in authorization request. |
|
|
78
|
+
| `EventSource` | `typeof EventSource` | `globalThis.EventSource` | Constructor used for the shared connection and independent streams. |
|
|
79
|
+
| `withCredentials` | `boolean` | `false` | Set to `true` to pass `{ withCredentials: true }` to `EventSource` for the shared connection and streams. |
|
|
80
|
+
|
|
81
|
+
## Authorization
|
|
82
|
+
|
|
83
|
+
For each private channel that needs a token, the client sends a JSON `POST` to `authEndpoint` with a body such as `{ "channel_name": "private-orders.42" }`. Once the shared connection ID is known, the body also includes `socket_id`. Up to four requests run concurrently per connection attempt. A change that replaces a pending attempt or closes the client aborts its outstanding requests. By default the client uses `credentials: 'include'`, the `Accept: application/json` and `Content-Type: application/json` headers, and the URL-decoded `XSRF-TOKEN` cookie as `X-XSRF-TOKEN` when the cookie is readable. For Laravel session authentication, make the session and CSRF cookies available and configure CORS if the endpoint is cross-origin.
|
|
84
|
+
|
|
85
|
+
`auth.headers` accepts a `Record<string, string>` or a function that returns one synchronously or asynchronously. The client calls the function for each token request and merges the headers over the defaults case-insensitively. A supplied `X-XSRF-TOKEN` takes precedence over the cookie. `auth.credentials` is a Fetch `RequestCredentials` value and defaults to `'include'`. Setting `credentials: 'omit'` skips reading the `XSRF-TOKEN` cookie. Use `withCredentials` to configure credentials on the SSE connections.
|
|
86
|
+
|
|
87
|
+
For an API that uses bearer tokens, read the current access token when authorization runs.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
const murmur = new Murmur({
|
|
91
|
+
url: 'https://events.example.com/sse',
|
|
92
|
+
authEndpoint: 'https://api.example.com/realtime/token',
|
|
93
|
+
auth: {
|
|
94
|
+
credentials: 'omit',
|
|
95
|
+
headers: () => {
|
|
96
|
+
const token = sessionStorage.getItem('access_token')
|
|
97
|
+
if (!token) {
|
|
98
|
+
throw new Error('Sign in to authorize private channels')
|
|
99
|
+
}
|
|
100
|
+
return { Authorization: `Bearer ${token}` }
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
A successful request returns `{ token, expires_at }`, with expiry in Unix seconds. The client caches credentials by private channel and normally refreshes them when their remaining lifetime reaches `tokenRefreshMarginSeconds` or less on a new attempt. Leaving a topic drops its cached credentials.
|
|
107
|
+
|
|
108
|
+
The client counts SSE failures before the EventSource opens when the attempt sent credentials and the failure either reports HTTP 401 or 403 or exposes no status. Native EventSource does not expose HTTP status. The client drops the credentials sent on the second, fourth, eighth, and subsequent power-of-two failures. If the first counted failure exposes 401 or 403, the client drops credentials immediately and then on the fourth, eighth, and subsequent power-of-two failures. Known failures with other statuses leave this count and the cache unchanged. An attach marker resets the schedule. The normal expiry margin still applies on every attempt.
|
|
109
|
+
|
|
110
|
+
Authorization responses with status 408, 425, or 429 retry with backoff, as do 5xx responses, network errors, and invalid token responses. A valid `Retry-After` header in whole seconds or IMF-fixdate form, such as `Sun, 06 Nov 1994 08:49:37 GMT`, sets a minimum delay for the failed attempt, capped at `maxRetryMs`. Other 4xx responses, including 401, 403, 419, and 422, leave the affected channel out of the shared connection. Call `murmur.reauthorize('orders.42')` to retry a denied channel, call `murmur.reauthorize()` to retry all denied channels, or leave and rejoin the channel.
|
|
111
|
+
|
|
112
|
+
To control how tokens are requested, pass an `authorizer(channels, signal)` function. Whenever an eligible private topic needs credentials, the function receives all eligible private wire topic names, including those with cached credentials. A rejection with a numeric 4xx `status`, except for 408, 425, and 429, denies only the topics without valid cached credentials. The client can keep unexpired batch credentials on a rejection even if it was attempting to refresh them. Other rejections retry with backoff. Reject with the HTTP `Response` to pass its `Retry-After` header through to the retry logic. The custom authorizer takes over requests from `authEndpoint`, `auth`, and the client's `fetch` option.
|
|
113
|
+
|
|
114
|
+
Resolve with one token result or an array of results. Use `{ token, expires_at, channels }` to declare the full wire topic names covered by each token. When `channels` is omitted, the client treats the token as covering the whole batch. Multiple tokens are cached as a group only when every token in the result omits `channels`. The result must cover every requested topic the application still grants. A successful result denies each uncovered topic with a 403, discards its cached credentials, and leaves it out of the SSE request. An empty result array denies the entire batch.
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
const murmur = new Murmur({
|
|
118
|
+
url: 'https://events.example.com/sse',
|
|
119
|
+
authorizer: async (channels, signal) => {
|
|
120
|
+
const response = await fetch('/api/realtime/authorize', {
|
|
121
|
+
method: 'POST',
|
|
122
|
+
headers: { 'Content-Type': 'application/json' },
|
|
123
|
+
body: JSON.stringify({ topics: channels }),
|
|
124
|
+
signal,
|
|
125
|
+
})
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
throw response
|
|
128
|
+
}
|
|
129
|
+
return response.json()
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The server defaults to 50 topics per connection and has an 8 KiB limit for the request line and headers. The client limits the UTF-8 byte length of the path and query to 6 KiB to reserve room for headers. It checks the largest possible reconnect URL, reserving a 32-character `connection` ID, a `lastEventId` with two 18-digit components, and a `fresh` list containing every topic.
|
|
135
|
+
|
|
136
|
+
| Example wire topic | Guaranteed client capacity | Largest reconnect URL path and query |
|
|
137
|
+
| --- | ---: | ---: |
|
|
138
|
+
| `private-orders.0042` | 30 | 5,991 bytes |
|
|
139
|
+
| `private-orders.00000000-0000-4000-8000-000000000042` | 19 | 5,868 bytes |
|
|
140
|
+
| `private-a` | 37 | 6,142 bytes |
|
|
141
|
+
|
|
142
|
+
These measurements use `/sse`, distinct topics of each example's length, the client's URL encoding, the full reconnect reservation, and one default-shaped HS256 JWT per topic with a 10-digit Unix expiry. The guaranteed capacity is lower than what a single request can carry. Extra query parameters or longer tokens take up part of the same budget. If a subscription exceeds that budget, the client emits `error` and waits for a subscription change. For pages with many private channels, configure a custom `authorizer` that returns one multi-topic token.
|
|
143
|
+
|
|
144
|
+
## Streams
|
|
145
|
+
|
|
146
|
+
Pass the subscription URL returned by Laravel's `$stream->url()` to `murmur.stream(url)`. Each stream opens its own connection.
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
const stream = murmur.stream(subscriptionUrl)
|
|
150
|
+
.on('token', data => console.log(data))
|
|
151
|
+
.on('end', () => console.log('Stream finished'))
|
|
152
|
+
.on('gap', () => console.log('Some events are missing'))
|
|
153
|
+
.on('expired', () => console.log('Stream stopped after repeated failures'))
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Stream event names are literal, and callbacks receive the event payload. The names `end`, `gap`, and `expired` are reserved for the control callbacks, so a server event with one of those names cannot be consumed through a stream. The `end`, `gap`, and `expired` callbacks take no arguments.
|
|
157
|
+
|
|
158
|
+
- `end` fires when the server sends `murmur.end`. The stream closes permanently.
|
|
159
|
+
- `gap` fires when the server sends `murmur.gap` to signal missing events.
|
|
160
|
+
- `expired` fires and the stream closes after `maxFailures` consecutive attempts that end without attachment or replay progress. A replayed event that advances the cursor resets the expiry count. Retry backoff keeps growing until the attach marker. A drop after attachment starts a new retry sequence and leaves the full allowance of eight attempts by default. A private stream URL carries a fixed token, so retries keep using that token.
|
|
161
|
+
|
|
162
|
+
With the defaults and immediate failures, expiry takes 135–168.75 seconds when the first connection never attaches, or 165–206.25 seconds after an attached connection drops. These ranges include the client retry delays and jitter. Time spent waiting for connections to open or fail adds to the total. Set `streamMaxFailures` on the client or `maxFailures` on an individual stream to change the number of attempts. Tune `retryMs`, `maxRetryMs`, and `retryJitter` to change the delay between attempts.
|
|
163
|
+
|
|
164
|
+
Pass `StreamOptions` as the optional second argument to `murmur.stream(url, options)`. Each field is an optional number and follows the validation rules of its client default.
|
|
165
|
+
|
|
166
|
+
| Field | Type | Default | Meaning |
|
|
167
|
+
| --- | --- | --- | --- |
|
|
168
|
+
| `retryMs` | `number` | Client's `retryMs` | Base delay before the first retry in milliseconds. |
|
|
169
|
+
| `maxRetryMs` | `number` | Client's `maxRetryMs`, or this stream's `retryMs` when that is larger | Cap on the base retry delay before jitter, in milliseconds. Must be at least this stream's `retryMs`. |
|
|
170
|
+
| `retryJitter` | `number` | Client's `retryJitter` | Maximum jitter fraction on each retry. |
|
|
171
|
+
| `maxFailures` | `number` | Client's `streamMaxFailures` | Limit on consecutive attempts without attachment or replay progress for this stream. |
|
|
172
|
+
|
|
173
|
+
The first connection uses the URL as supplied. On reconnect, the client updates only the `lastEventId` query parameter. The read-only `stream.lastEventId` starts with the URL's valid cursor, if present, and tracks IDs from listened-to events, attach markers, and numbered heartbeats after attachment. It is `undefined` when the stream has no cursor and remains readable after the stream closes.
|
|
174
|
+
|
|
175
|
+
To resume after `expired`, get a subscription URL for the same stream from your application and replace its `lastEventId` query parameter with the saved cursor. A fresh URL also provides a fresh token for a private stream. Pass the updated URL to `murmur.stream()` and register the listeners on the new stream. A new EventSource starts without a `Last-Event-ID` header. If a custom transport sends a valid `Last-Event-ID` header, the server gives it precedence over the query parameter.
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
function consumeStream(subscriptionUrl, lastEventId) {
|
|
179
|
+
const url = new URL(subscriptionUrl, window.location.href)
|
|
180
|
+
if (lastEventId !== undefined) {
|
|
181
|
+
url.searchParams.set('lastEventId', lastEventId)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const stream = murmur.stream(url.href)
|
|
185
|
+
.on('token', data => console.log(data))
|
|
186
|
+
.on('gap', () => console.log('Some events are missing'))
|
|
187
|
+
|
|
188
|
+
stream.on('expired', async () => {
|
|
189
|
+
try {
|
|
190
|
+
consumeStream(await getSubscriptionUrl(), stream.lastEventId)
|
|
191
|
+
} catch (error) {
|
|
192
|
+
console.error('Could not resume the stream', error)
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
return stream
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
consumeStream(await getSubscriptionUrl())
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
This example resumes every time the stream expires, without a limit. Add your own limit or delay if the application should eventually give up.
|
|
202
|
+
|
|
203
|
+
Call `stream.close()` to stop the stream and cancel any pending retry.
|
|
204
|
+
|
|
205
|
+
## Statistics
|
|
206
|
+
|
|
207
|
+
`murmur.stats()` returns a fresh `MurmurStats` snapshot. Cumulative counters survive reconnects. Closing the client clears the live state and preserves the final counters, connection ID, replay cursor, and event timestamp.
|
|
208
|
+
|
|
209
|
+
| Field | Type | Meaning |
|
|
210
|
+
| --- | --- | --- |
|
|
211
|
+
| `connected` | `boolean` | Whether the shared connection is open. |
|
|
212
|
+
| `connections` | `number` | How many times the shared connection opened. |
|
|
213
|
+
| `disconnections` | `number` | Shared connection failures that enter backoff and unexpected drops. |
|
|
214
|
+
| `events` | `number` | Valid data envelopes received on a listened-to shared event name and passed to channel dispatch, once per received event. |
|
|
215
|
+
| `gaps` | `number` | `murmur.gap` signals received on the shared connection. |
|
|
216
|
+
| `tokenRequests` | `number` | Authorization attempts, including custom authorizer calls and attempts cancelled before completing. Reusing a cached token skips the request. |
|
|
217
|
+
| `tokenFailures` | `number` | Authorization attempts that failed before cancellation, including invalid token responses. An accepted partial grant is a successful request. |
|
|
218
|
+
| `channels` | `number` | Currently joined channels. |
|
|
219
|
+
| `streams` | `number` | Independent streams that have started and have yet to close, including streams waiting to retry. |
|
|
220
|
+
| `lastEventId` | `string \| undefined` | Shared replay cursor. It tracks IDs from attach markers, listened-to data events, and numbered heartbeats after attachment. |
|
|
221
|
+
| `connectionId` | `string \| undefined` | The most recently announced ID of the shared connection. |
|
|
222
|
+
| `lastEventAt` | `number \| undefined` | `Date.now()` when the last valid shared data event was received. |
|
|
223
|
+
|
|
224
|
+
```js
|
|
225
|
+
murmur.on('disconnected', () => {
|
|
226
|
+
const { disconnections, lastEventAt } = murmur.stats()
|
|
227
|
+
console.log({ disconnections, lastEventAt })
|
|
228
|
+
})
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Laravel Echo
|
|
232
|
+
|
|
233
|
+
Install Murmur and Laravel Echo 2.6.3 or a later 2.x release.
|
|
234
|
+
|
|
235
|
+
```sh
|
|
236
|
+
npm install @arttecs/murmur laravel-echo@^2.6.3
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Pass `MurmurConnector` as the Echo broadcaster.
|
|
240
|
+
|
|
241
|
+
```js
|
|
242
|
+
import Echo from 'laravel-echo'
|
|
243
|
+
import { MurmurConnector } from '@arttecs/murmur/echo'
|
|
244
|
+
|
|
245
|
+
const echo = new Echo({
|
|
246
|
+
broadcaster: MurmurConnector,
|
|
247
|
+
url: 'https://events.example.com/sse',
|
|
248
|
+
authEndpoint: '/broadcasting/auth',
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
echo.private('orders.1')
|
|
252
|
+
.listen('OrderShipped', event => console.log(event))
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
| Echo option | Murmur behavior |
|
|
256
|
+
| --- | --- |
|
|
257
|
+
| `broadcaster` | Set to `MurmurConnector` to use SSE. |
|
|
258
|
+
| `url` | Required SSE endpoint. |
|
|
259
|
+
| `authEndpoint` | Private token endpoint. Defaults to `/broadcasting/auth`. |
|
|
260
|
+
| `auth.headers`, `auth.credentials` | Headers and Fetch credential mode for the token request. |
|
|
261
|
+
| `csrfToken` | CSRF token to use when `window.Laravel.csrfToken` is empty or absent. |
|
|
262
|
+
| `bearerToken` | A non-empty token sets `Authorization: Bearer <token>`, replacing any supplied authorization header. |
|
|
263
|
+
| `authorizer` | Replaces the built-in token request. Accepts private wire topic names and an abort signal. |
|
|
264
|
+
| `withCredentials` | Sends browser credentials with SSE requests. |
|
|
265
|
+
| `namespace` | Defaults to `App.Events`. Set to `false` to omit the prefix. |
|
|
266
|
+
| `retryMs`, `maxRetryMs`, `retryJitter` | Control the retry delay with the same defaults as the standalone client. |
|
|
267
|
+
| `tokenRefreshMarginSeconds` | Controls when a cached private token needs refreshing. |
|
|
268
|
+
| `streamMaxFailures` | Sets the failure limit for independent streams opened on the underlying client. |
|
|
269
|
+
| `fetch`, `EventSource` | Override the Fetch implementation for token requests and the EventSource constructor for SSE. |
|
|
270
|
+
|
|
271
|
+
The built-in token request uses Murmur's JSON and CSRF cookie conventions. For each built-in token request, the connector resolves `X-CSRF-TOKEN` from the first non-empty value among `window.Laravel.csrfToken`, the `csrfToken` option, and the `content` attribute of `<meta name="csrf-token">`. That value overrides a supplied `X-CSRF-TOKEN` header. After a 419, refresh the application's CSRF token and call `echo.connector.murmur.reauthorize(name)` with the private channel name. A custom `authorizer` handles its own headers. Echo 2.6.3's TypeScript declarations specify static authorization headers. To pass a header provider or set `auth.credentials`, build a `MurmurConnectorOptions` value and cast the Echo options. [Authorization options](#authorization)
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
import Echo, { type EchoOptions } from 'laravel-echo'
|
|
275
|
+
import { MurmurConnector, type MurmurConnectorOptions } from '@arttecs/murmur/echo'
|
|
276
|
+
|
|
277
|
+
const options: MurmurConnectorOptions = {
|
|
278
|
+
url: 'https://events.example.com/sse',
|
|
279
|
+
auth: { credentials: 'omit', headers: () => ({ Authorization: `Bearer ${currentToken()}` }) },
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const echo = new Echo<'function'>({ broadcaster: MurmurConnector, ...options } as EchoOptions<'function'>)
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Laravel Echo 2.6.3's axios, Vue, jQuery, and Turbo interceptors send the announced ID as `X-Socket-ID` once it is available. Before the first announcement, `echo.socketId()` returns an empty string. The first three interceptors skip the header in that case, while Turbo sends an empty header value.
|
|
286
|
+
|
|
287
|
+
Public channels, private channels, and Laravel broadcast notifications share one SSE connection. Event names follow Echo's formatter. For example, `Sub.OrderShipped` resolves to `App\Events\Sub\OrderShipped`, while `.order.shipped` listens for the literal `order.shipped`. An initial backslash also marks a literal event name.
|
|
288
|
+
|
|
289
|
+
- `stopListening(event, callback)` removes one callback. Omit the callback to remove all listeners for the event. `stopListeningForNotification(callback)` removes one notification callback.
|
|
290
|
+
- Register `subscribed(callback)` before attachment to receive the initial notification. It fires on the first `murmur.attached` that includes the channel and on successful attachment after a connection failure. Joining or leaving another channel preserves the existing channels' subscription state. Wait for this callback before fetching initial state.
|
|
291
|
+
- On a private channel, `error(callback)` receives that channel's numeric authorization denial status. Other authorized and public channels remain eligible for the shared connection. Retry a denied channel through `echo.connector.murmur.reauthorize(name)` or by leaving and rejoining it.
|
|
292
|
+
- `leave('orders.1')` leaves both public and private variants. `leaveChannel('private-orders.1')` leaves just the private one. The `private-` prefix identifies an authorized topic.
|
|
293
|
+
- `echo.connector.murmur` exposes the underlying client for statistics, streams, and replay gaps. In TypeScript, read `echo.connector` into an `unknown` variable and narrow it with `instanceof MurmurConnector` to access the typed client.
|
|
294
|
+
- `echo.disconnect()` closes the shared connection, cancels retries, and closes independent streams. Call `echo.connect()` to create a fresh connector, then rejoin the channels you need.
|
|
295
|
+
|
|
296
|
+
`connectionStatus()` reports the shared connection's state. `onConnectionChange(callback)` on the connector returns an unsubscribe function.
|
|
297
|
+
|
|
298
|
+
| Status | Meaning |
|
|
299
|
+
| --- | --- |
|
|
300
|
+
| `disconnected` | No shared connection is needed, the client is closed, or every joined topic is unauthorized. |
|
|
301
|
+
| `connecting` | The client is preparing or opening the stream after a subscription change or explicit reauthorization. |
|
|
302
|
+
| `connected` | The shared EventSource is open. Attachment is reported through `subscribed(callback)`. |
|
|
303
|
+
| `reconnecting` | An attempt failed or the connection dropped, and the client is retrying with backoff. |
|
|
304
|
+
| `failed` | The shared subscription URL exceeds the request budget. Change the subscription to try again. |
|
|
305
|
+
|
|
306
|
+
Murmur's Echo connector has these limitations.
|
|
307
|
+
|
|
308
|
+
- Presence and encrypted private channels throw an unsupported-channel error.
|
|
309
|
+
- Client events are unavailable. `whisper`, `listenForWhisper`, and `stopListeningForWhisper` are chainable no-ops; the first `whisper` or `listenForWhisper` call on a connector prints one console warning.
|
|
310
|
+
- Wildcard event listeners are unavailable. Register named events with `listen`.
|
|
311
|
+
|
|
312
|
+
`@laravel/echo-react` and `@laravel/echo-vue` 2.6.3 were checked once by hand with the built connector in jsdom using a fake EventSource; that check is not part of this repository's test suite. In both packages, mounted consumers received public events, private events, and notifications. The connection-status hook reached `connected`, and unmounting released the channels and closed the connection.
|
|
313
|
+
|
|
314
|
+
## Development
|
|
315
|
+
|
|
316
|
+
- The build and test tooling requires Node.js `^22.18.0 || ^24.11.0 || >=26.0.0`.
|
|
317
|
+
- `npm test` runs the unit tests.
|
|
318
|
+
- `npm run test:integration` runs the client against the real Murmur server. It requires PHP on `PATH` and `composer install` at the repository root.
|
|
319
|
+
- `npm run build` builds the ESM package and its TypeScript declarations.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** The callbacks registered under each wire event name. */
|
|
2
|
+
export type ChannelListeners = Map<string, Set<(data: unknown) => void>>;
|
|
3
|
+
/** A subscription to one Murmur topic. */
|
|
4
|
+
export declare class Channel {
|
|
5
|
+
protected readonly namespace: string;
|
|
6
|
+
protected readonly listeners: ChannelListeners;
|
|
7
|
+
protected readonly listenersChanged: () => void;
|
|
8
|
+
/** Callbacks registered for this channel's authorization failures. */
|
|
9
|
+
protected readonly errors: Set<(status: number) => void>;
|
|
10
|
+
/** Callbacks registered for the server's attachment acknowledgment. */
|
|
11
|
+
protected readonly attachments: Set<() => void>;
|
|
12
|
+
/** The subscribed topic, including its prefix for private channels. */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Registers a callback for an event on this channel. */
|
|
15
|
+
listen(event: string, callback: (data: unknown) => void): this;
|
|
16
|
+
/** Removes the callbacks for an event on this channel. */
|
|
17
|
+
stopListening(event: string): this;
|
|
18
|
+
/** Registers a callback for a denied authorization request on this channel. */
|
|
19
|
+
error(callback: (status: number) => void): this;
|
|
20
|
+
/** Resolves an event to the name used in the stream. */
|
|
21
|
+
protected eventName(event: string): string;
|
|
22
|
+
}
|