Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 3058d0f

Browse files
filipecabacow3b6x9
andauthored
fix: Broadcast endpoint includes private param in request body (#414)
Co-authored-by: Wen Bo Xie <5532241+w3b6x9@users.noreply.github.com>
1 parent 176623d commit 3058d0f

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/RealtimeChannel.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export default class RealtimeChannel {
135135
presence: RealtimePresence
136136
broadcastEndpointURL: string
137137
subTopic: string
138+
private: boolean
138139

139140
constructor(
140141
/** Topic name can be any string. */
@@ -198,6 +199,7 @@ export default class RealtimeChannel {
198199

199200
this.broadcastEndpointURL =
200201
httpEndpointURL(this.socket.endPoint) + '/api/broadcast'
202+
this.private = this.params.config.private || false
201203
}
202204

203205
/** Subscribe registers your client with the server */
@@ -451,7 +453,12 @@ export default class RealtimeChannel {
451453
},
452454
body: JSON.stringify({
453455
messages: [
454-
{ topic: this.subTopic, event, payload: endpoint_payload },
456+
{
457+
topic: this.subTopic,
458+
event,
459+
payload: endpoint_payload,
460+
private: this.private,
461+
},
455462
],
456463
}),
457464
}

test/channel_test.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ describe('constructor', () => {
2424
assert.equal(channel.state, 'closed')
2525
assert.equal(channel.topic, 'topic')
2626
assert.deepEqual(channel.params, {
27-
config: { broadcast: { ack: false, self: false }, presence: { key: '' }, private: false },
27+
config: {
28+
broadcast: { ack: false, self: false },
29+
presence: { key: '' },
30+
private: false,
31+
},
2832
one: 'two',
2933
})
3034
assert.deepEqual(channel.socket, socket)
@@ -40,7 +44,11 @@ describe('constructor', () => {
4044

4145
assert.deepEqual(joinPush.channel, channel)
4246
assert.deepEqual(joinPush.payload, {
43-
config: { broadcast: { ack: false, self: false }, presence: { key: '' }, private: false },
47+
config: {
48+
broadcast: { ack: false, self: false },
49+
presence: { key: '' },
50+
private: false,
51+
},
4452
one: 'two',
4553
})
4654
assert.equal(joinPush.event, 'phx_join')
@@ -1232,7 +1240,7 @@ describe('send', () => {
12321240
socket = new RealtimeClient('ws://localhost:4000/socket', {
12331241
params: { apikey: 'abc123' },
12341242
})
1235-
channel = socket.channel('topic', { one: 'two' })
1243+
channel = socket.channel('topic', { one: 'two', config: { private: true } })
12361244
})
12371245

12381246
afterEach(() => {
@@ -1275,6 +1283,16 @@ describe('send', () => {
12751283
})
12761284

12771285
it('sends message via http request to Broadcast endpoint when not subscribed to channel', async () => {
1286+
const expectedBody = {
1287+
method: 'POST',
1288+
headers: {
1289+
Authorization: 'Bearer abc123',
1290+
apikey: 'abc123',
1291+
'Content-Type': 'application/json',
1292+
},
1293+
body: '{"messages":[{"topic":"topic","event":"test","private":true}]}',
1294+
}
1295+
12781296
pushStub = sinon.stub(channel, '_fetchWithTimeout')
12791297
pushStub.returns(new Response())
12801298

@@ -1285,7 +1303,12 @@ describe('send', () => {
12851303
})
12861304

12871305
assert.equal(res, 'ok')
1288-
assert.ok(pushStub.calledOnceWith('http://localhost:4000/api/broadcast'))
1306+
assert.ok(
1307+
pushStub.calledOnceWith(
1308+
'http://localhost:4000/api/broadcast',
1309+
expectedBody
1310+
)
1311+
)
12891312
})
12901313
})
12911314

0 commit comments

Comments
 (0)