Last active
February 26, 2026 10:33
-
-
Save devhammed/8ee080470250a99c8c2d385d820d5ace to your computer and use it in GitHub Desktop.
AdonisJS v7 Transmit Setup (you must uninstall the official package then register the custom provider and preload file).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { HttpContext } from '@adonisjs/core/http' | |
| import app from '@adonisjs/core/services/app' | |
| import router from '@adonisjs/core/services/router' | |
| import { RuntimeException } from '@poppinss/utils/exception' | |
| const transmit = await app.container.make('transmit') | |
| transmit.authorize<{ id: string }>('users/:id', async (ctx: HttpContext, params) => { | |
| const userId = ctx.auth.user?.id | |
| if (!userId) { | |
| return false | |
| } | |
| return userId === params.id | |
| }) | |
| router.get('/__transmit/events', async (ctx) => { | |
| const uid = ctx.request.input('uid') | |
| if (!uid) { | |
| throw new RuntimeException('Missing required field "uid" in the request body') | |
| } | |
| const stream = transmit.createStream({ | |
| uid, | |
| context: ctx, | |
| request: ctx.request.request, | |
| response: ctx.response.response, | |
| injectResponseHeaders: ctx.response.getHeaders(), | |
| }) | |
| return ctx.response.stream(stream) | |
| }) | |
| router.post('/__transmit/subscribe', async (ctx) => { | |
| const uid = ctx.request.input('uid') | |
| const channel = ctx.request.input('channel') | |
| const success = await transmit.subscribe({ | |
| uid, | |
| channel, | |
| context: ctx, | |
| }) | |
| if (!success) { | |
| return ctx.response.badRequest() | |
| } | |
| return ctx.response.noContent() | |
| }) | |
| router.post('/__transmit/unsubscribe', async (ctx) => { | |
| const uid = ctx.request.input('uid') | |
| const channel = ctx.request.input('channel') | |
| const success = await transmit.unsubscribe({ | |
| uid, | |
| channel, | |
| context: ctx, | |
| }) | |
| if (!success) { | |
| return ctx.response.badRequest() | |
| } | |
| return ctx.response.noContent() | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { ApplicationService } from '@adonisjs/core/types' | |
| import type { HttpContext } from '@adonisjs/core/http' | |
| import { Transmit } from '@boringnode/transmit' | |
| import { TransmitConfig } from '@boringnode/transmit/types' | |
| declare module '@adonisjs/core/types' { | |
| interface ContainerBindings { | |
| 'transmit': Transmit<HttpContext> | |
| } | |
| } | |
| export default class TransmitProvider { | |
| constructor(protected app: ApplicationService) {} | |
| /** | |
| * Register bindings to the container | |
| */ | |
| register() { | |
| this.app.container.singleton('transmit', async () => { | |
| const config = this.app.config.get<TransmitConfig>('transmit', {}) | |
| let transport: Transport | null = null | |
| if (config.transport) { | |
| transport = config.transport.driver() | |
| } | |
| return new Transmit(config, transport) | |
| }) | |
| } | |
| /** | |
| * The container bindings have booted | |
| */ | |
| async boot() { | |
| // | |
| } | |
| /** | |
| * The application has been booted | |
| */ | |
| async start() { | |
| // | |
| } | |
| /** | |
| * The process has been started | |
| */ | |
| async ready() { | |
| // | |
| } | |
| /** | |
| * Preparing to shut down the app | |
| */ | |
| async shutdown() { | |
| const transmit = await this.app.container.make('transmit') | |
| await transmit.shutdown() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment