Class: Broadcast<T, S>
Creates a Broadcast workloads
The constructore takes a session handler and a configuration object for channels. Each channel is mapped to a path and an optional authorization handler. The channel paths can include dynamic segments, like /users/[id]
, which are parsed and passed to the authorization logic.
Param
A function that resolves to a session object. This object is available within the authorization handlers. It receives an object with cookies
as an argument.
Param
An object where keys are route strings and values are objects defining the channel and its authorization logic.
auth
: An optional function to authorize "PUBLISH" or "SUBSCRIBE" operations. It receives the operation type, route parameters, and the session object.channel
: The channel instance for the route.
Example
import { channel, Broadcast } from "@monolayer/sdk";
const broadcast = new Broadcast(
async () => ({ userId: 1 }),
{
"/todos": {
channel: channel<{ message: string }>(),
auth: async (ctx) => {
console.log("User trying to access todos:", ctx.session.userId);
return true; // Allow access
},
},
"/users/[id]": {
channel: channel<{ salute: string }>(),
auth: async (ctx) => {
// Only allow users to access their own channel
return ctx.session.userId === Number(ctx.params.id);
},
},
}
);
export default broadcast;
Extends
Type Parameters
Type Parameter | Description |
---|---|
| A record mapping channel strings to their channel data types. |
| The type of the session object, returned by the |
Accessors
connectionStringEnvVar
Get Signature
get connectionStringEnvVar(): "ML_BROADCAST_URL"
Returns
"ML_BROADCAST_URL"
Constructors
new Broadcast()
new Broadcast<T, S>(initOpts): Broadcast<T, S>
Parameters
Parameter | Type |
---|---|
|
|
| { [channel in string | number | symbol]: ValidateRoute<channel & string> extends never ? never : Object } |
| ( |
Returns
Broadcast
<T
, S
>
Overrides
Methods
authFn()
authFn(
route,
operation,
session): Promise<boolean>
Parameters
Parameter | Type |
---|---|
|
|
|
|
|
|
Returns
Promise
<boolean
>
Properties
Property | Modifier | Type | Description |
---|---|---|---|
_channelDataType | public | { [K in string | number | symbol]: T[K] } | - |
channels | public | { [channel in string | number | symbol]: ValidateRoute<channel & string> extends never ? never : Object } | - |
id | readonly | string | Unique ID |
session | public | (opts : object ) => Promise <S > | - |