Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release new realtime-js version #408

Merged
merged 23 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
"typedoc": "^0.22.16",
"typescript": "^4.0.3"
}
}
}
24 changes: 14 additions & 10 deletions src/RealtimeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
RealtimePresenceState,
} from './RealtimePresence'
import * as Transformers from './lib/transformers'
import { httpEndpointURL } from './lib/transformers'

export type RealtimeChannelOptions = {
config: {
Expand All @@ -23,6 +24,10 @@ export type RealtimeChannelOptions = {
* key option is used to track presence payload across clients
*/
presence?: { key?: string }
/**
* defines if the channel is private or not and if RLS policies will be used to check data
*/
private?: boolean
}
}

Expand Down Expand Up @@ -138,11 +143,11 @@ export default class RealtimeChannel {
public socket: RealtimeClient
) {
this.subTopic = topic.replace(/^realtime:/i, '')

this.params.config = {
...{
broadcast: { ack: false, self: false },
presence: { key: '' },
private: false,
},
...params.config,
}
Expand Down Expand Up @@ -191,7 +196,8 @@ export default class RealtimeChannel {

this.presence = new RealtimePresence(this)

this.broadcastEndpointURL = this._broadcastEndpointURL()
this.broadcastEndpointURL =
httpEndpointURL(this.socket.endPoint) + '/api/broadcast'
}

/** Subscribe registers your client with the server */
Expand All @@ -207,7 +213,7 @@ export default class RealtimeChannel {
throw `tried to subscribe multiple times. 'subscribe' can only be called a single time per channel instance`
} else {
const {
config: { broadcast, presence },
config: { broadcast, presence, private: isPrivate },
} = this.params
this._onError((e: Error) => callback && callback('CHANNEL_ERROR', e))
this._onClose(() => callback && callback('CLOSED'))
Expand All @@ -218,6 +224,7 @@ export default class RealtimeChannel {
presence,
postgres_changes:
this.bindings.postgres_changes?.map((r) => r.filter) ?? [],
private: isPrivate,
}

if (this.socket.accessToken) {
Expand Down Expand Up @@ -436,7 +443,10 @@ export default class RealtimeChannel {
const options = {
method: 'POST',
headers: {
apikey: this.socket.apiKey ?? '',
Authorization: this.socket.accessToken
? `Bearer ${this.socket.accessToken}`
: '',
apikey: this.socket.apiKey ? this.socket.apiKey : '',
'Content-Type': 'application/json',
},
body: JSON.stringify({
Expand Down Expand Up @@ -529,12 +539,6 @@ export default class RealtimeChannel {
}

/** @internal */
_broadcastEndpointURL(): string {
let url = this.socket.endPoint
url = url.replace(/^ws/i, 'http')
url = url.replace(/(\/socket\/websocket|\/socket|\/websocket)\/?$/i, '')
return url.replace(/\/+$/, '') + '/api/broadcast'
}

async _fetchWithTimeout(
url: string,
Expand Down
29 changes: 20 additions & 9 deletions src/RealtimeClient.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import type { WebSocket as WSWebSocket } from 'ws'

import {
VSN,
CHANNEL_EVENTS,
TRANSPORTS,
SOCKET_STATES,
CONNECTION_STATE,
DEFAULT_HEADERS,
DEFAULT_TIMEOUT,
SOCKET_STATES,
TRANSPORTS,
VSN,
WS_CLOSE_NORMAL,
DEFAULT_HEADERS,
CONNECTION_STATE,
} from './lib/constants'
import Timer from './lib/timer'
import Serializer from './lib/serializer'
import Timer from './lib/timer'

import { httpEndpointURL } from './lib/transformers'
import RealtimeChannel from './RealtimeChannel'
import type { RealtimeChannelOptions } from './RealtimeChannel'

import type { WebSocket as WSWebSocket } from 'ws'

type Fetch = typeof fetch

export type Channel = {
name: string
inserted_at: string
updated_at: string
id: number
}

Comment on lines +22 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filipecabaco It seems like this type isn't being used, but what is it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will check with @w3b6x9 🤔

export type RealtimeClientOptions = {
transport?: WebSocketLikeConstructor
timeout?: number
Expand Down Expand Up @@ -66,6 +75,7 @@ export default class RealtimeClient {
apiKey: string | null = null
channels: RealtimeChannel[] = []
endPoint: string = ''
httpEndpoint: string = ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this parameter too. Is there another PR that makes use of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be used for http broadcast (aka non ws connection broadcast for edge functions). It will also be useful for longpoll fallback

headers?: { [key: string]: string } = DEFAULT_HEADERS
params?: { [key: string]: string } = {}
timeout: number = DEFAULT_TIMEOUT
Expand Down Expand Up @@ -99,6 +109,7 @@ export default class RealtimeClient {
* Initializes the Socket.
*
* @param endPoint The string WebSocket endpoint, ie, "ws://example.com/socket", "wss://example.com", "/socket" (inherited host & protocol)
* @param httpEndpoint The string HTTP endpoint, ie, "https://example.com", "/" (inherited host & protocol)
* @param options.transport The Websocket Transport, for example WebSocket.
* @param options.timeout The default timeout in milliseconds to trigger push timeouts.
* @param options.params The optional params to pass when connecting.
Expand All @@ -111,7 +122,7 @@ export default class RealtimeClient {
*/
constructor(endPoint: string, options?: RealtimeClientOptions) {
this.endPoint = `${endPoint}/${TRANSPORTS.websocket}`

this.httpEndpoint = httpEndpointURL(endPoint)
if (options?.transport) {
this.transport = options.transport
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,10 @@ export const toTimestampString = (value: RecordValue): RecordValue => {

return value
}

export const httpEndpointURL = (socketUrl: string): string => {
let url = socketUrl
url = url.replace(/^ws/i, 'http')
url = url.replace(/(\/socket\/websocket|\/socket|\/websocket)\/?$/i, '')
return url.replace(/\/+$/, '')
}
Loading
Loading