Node.js (server-side)
npm install pusherTrigger an event
import Pusher from 'pusher';
const pusher = new Pusher({
appId: process.env.MAWJLY_APP_ID!,
key: process.env.MAWJLY_KEY!,
secret: process.env.MAWJLY_SECRET!,
cluster: 'sa',
host: 'ws-sa.mawjly.com',
port: '443',
useTLS: true,
});
await pusher.trigger('chat', 'message', {
user: 'alice',
text: 'Hello from Node!',
});Batch trigger
await pusher.triggerBatch([
{ channel: 'chat', name: 'message', data: { text: 'one' } },
{ channel: 'chat', name: 'message', data: { text: 'two' } },
{ channel: 'chat', name: 'typing', data: { user: 'alice' } },
]);Channel info
const channels = await pusher.get({
path: '/channels',
params: { filter_by_prefix: 'presence-' },
});
const members = await pusher.get({
path: '/channels/presence-room-1/users',
});Auth endpoint (Express example)
import express from 'express';
const app = express();
app.use(express.urlencoded({ extended: true }));
app.post('/pusher/auth', (req, res) => {
const { socket_id, channel_name } = req.body;
// 1. Auth your user via your normal session middleware first.
const userId = req.session?.userId;
if (!userId) return res.sendStatus(401);
// 2. Optionally check the user can subscribe to this channel.
if (channel_name.startsWith('private-user.') &&
channel_name !== `private-user.${userId}`) {
return res.sendStatus(403);
}
// 3. Sign and return.
const presence = channel_name.startsWith('presence-')
? { user_id: String(userId), user_info: { name: req.session.name } }
: undefined;
const auth = pusher.authorizeChannel(socket_id, channel_name, presence);
res.json(auth);
});