Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@adonisjs/transmit
Advanced tools
A native Server-Sent-Event (SSE) module for AdonisJS.
AdonisJS Transmit is a native Server-Sent-Event (SSE) module for AdonisJS. It provides a simple API to send events to the client. It also supports Redis as a Transport Layer for broadcasting events to multiple servers or instances.
Here are a few things you should know before using this module.
👉 Unidirectional Communication: The data transmission occurs only from server to client, not the other way around.
👉 Textual Data Only: SSE only supports the transmission of textual data, binary data cannot be sent.
👉 HTTP Protocol: The underlying protocol used is the regular HTTP, not any special or proprietary protocol.
Install the package from the npm registry as follows:
node ace add @adonisjs/transmit
The module exposes a transmit
instance, which can be used to send events to the client.
import transmit from '@adonisjs/transmit/services/main'
// Anywhere in your code
transmit.broadcast('channelName', { username: 'lanz' })
Channels are a way to group events. For example, you can have a channel for users
and another for posts
. The client can subscribe to one or more channels to receive events.
Channels names must be a string and must not contain any special characters except /
. The following are valid channel names.
transmit.broadcast('users', { username: 'lanz' })
transmit.broadcast('users/1', { username: 'lanz' })
transmit.broadcast('users/1/posts', { username: 'lanz' })
You can mark a channel as private and then authorize the client to subscribe to it. The authorization is done using a callback function.
// start/transmit.ts
import type { HttpContext } from '@adonisjs/core/http'
transmit.authorizeChannel<{ id: string }>('users/:id', (ctx: HttpContext, { id }) => {
return ctx.auth.user?.id === +id
})
[!NOTE] Do not forget to add your
start/transmit.ts
file inside thepreloads
array of theadonisrc.ts
file.
When a client tries to subscribe to a private channel, the callback function is invoked with the channel params and the HTTP context. The callback function must return a boolean value to allow or disallow the subscription.
Transmit supports syncing events across multiple servers or instances using a transport layer. You can enable syncing by changing the configuration and referencing your driver (only Redis is available as of now).
// config/transmit.ts
import env from '#start/env'
import { defineConfig } from '@adonisjs/transmit'
import { redis } from '@adonisjs/transmit/transports'
export default defineConfig({
transport: {
driver: redis({
host: env.get('REDIS_HOST'),
port: env.get('REDIS_PORT'),
password: env.get('REDIS_PASSWORD'),
})
}
})
[!NOTE] Ensure to have
ioredis
installed when using theredis
driver.
Transmit supports pinging the client to keep the connection alive. You can enable pinging by changing the configuration.
// config/transmit.ts
import { defineConfig } from '@adonisjs/transmit'
import { redis } from '@adonisjs/transmit/transports'
export default defineConfig({
pingInterval: '1m',
})
Transmit uses Emittery to emit any lifecycle events. You can listen for events using the on
method.
transmit.on('connect', ({ uid }) => {
console.log(`Connected: ${uid}`)
})
transmit.on('disconnect', ({ uid }) => {
console.log(`Disconnected: ${uid}`)
})
transmit.on('broadcast', ({ channel }) => {
console.log(`Broadcasted to channel ${channel}`)
})
transmit.on('subscribe', ({ uid, channel }) => {
console.log(`Subscribed ${uid} to ${channel}`)
})
transmit.on('unsubscribe', ({ uid, channel }) => {
console.log(`Unsubscribed ${uid} from ${channel}`)
})
FAQs
A Server-Sent-Event module bringing real-time capabilities to AdonisJS.
The npm package @adonisjs/transmit receives a total of 414 weekly downloads. As such, @adonisjs/transmit popularity was classified as not popular.
We found that @adonisjs/transmit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.