Implementing Private Channel Push with Webman Push (PHP & JavaScript)
This guide walks through installing the Webman framework and push plugin, configuring private and presence channels, creating authorization endpoints in PHP, and using JavaScript to subscribe and receive secure real‑time messages, complete with code examples and troubleshooting tips.
Installation
1. Install the Webman framework:
composer create-project workerman/webman webman20242. Install the push plugin:
composer require webman/pushNote: The plugin requires webman-framework >= 1.2.0 .
3. Start Webman. The HTTP server listens on port 8787 and the WebSocket server on port 8788: http 8787 websocket 8788
Client‑Side Usage (JavaScript)
Create a controller method privatePush that returns the view:
public function privatePush(Request $request)
{
return view('index/private_push');
}Place private_push.html in webman2024/app/view/index and include the push script:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Webman Push Private Channel</title>
</head>
<body>
<h2>如何使用Webman Push服务端私有频道推送</h2>
<script src="/plugin/webman/push/push.js"></script>
<script>
/** 1. Create instance */
const connection = new Push({
url: 'ws://127.0.0.1:8788', // WebSocket address
app_key: '216e96a3506044bfd2b8b6eb7c492d32',
auth: 'http://127.0.0.1:8787/index/private-auth' // auth URL for private channels
});
/** 2. Subscribe private channel, assuming user id = 2024 */
var uid = 2024;
var privateChannel = connection.subscribe('private-user-' + uid);
// Bind message event
privateChannel.on('message', function(data) {
alert(JSON.stringify(data));
});
</script>
</body>
</html>When a client subscribes to a channel prefixed with private- , the browser sends an AJAX request to the auth URL; the server must verify the user’s permission before allowing the subscription.
Authorization Endpoint (PHP)
Add a privateAuth method in webman2024/app/controller:
public function privateAuth(Request $request)
{
$pusher = new \Webman\Push\Api(
'http://127.0.0.1:3232',
config('plugin.webman.push.app.app_key'),
config('plugin.webman.push.app.app_secret')
);
$channelName = $request->post('channel_name');
$session = $request->session();
// TODO: validate $session and $channelName
$has_authority = true;
if ($has_authority) {
return response($pusher->socketAuth($channelName, $request->post('socket_id')));
} else {
return response('Forbidden', 403);
}
}Authorization request example:
{
"channel_name": "private-user-2024"
}Successful response example:
{
"auth": "216e96a3506044bfd2b8b6eb7c492d32:e2db027aa666198f3794f280be3a781131cc62a36738ecb3a3fd4c2a0371d6a3"
}Server‑Side Push (PHP)
Create a privatePushServer method to broadcast a message to the private channel:
public function privatePushServer(Request $request)
{
$api = new \Webman\Push\Api(
'http://127.0.0.1:3232',
config('plugin.webman.push.app.app_key'),
config('plugin.webman.push.app.app_secret')
);
$api->trigger('private-user-2024', 'message', [
'from_uid' => 2,
'content' => 'Hi! This is a private channel push message.'
]);
return 1;
}Invoke the endpoint http://127.0.0.1:8787/index/privatePushServer to send the message; the client will receive an alert with the payload.
Presence Channels
Presence channels extend private channels by exposing which users are currently subscribed. The implementation is similar; the channel name must start with presence- and the same auth URL is used.
Authorization method (same as private, but using presenceAuth):
public function privateAuth(Request $request)
{
$pusher = new \Webman\Push\Api(
'http://127.0.0.1:3232',
config('plugin.webman.push.app.app_key'),
config('plugin.webman.push.app.app_secret')
);
$channelName = $request->post('channel_name');
$has_authority = true;
if ($has_authority) {
$presenceData = ['name' => '开源技术小栈', 'age' => 24];
return response($pusher->presenceAuth($channelName, $request->post('socket_id'), 2024, $presenceData));
} else {
return response('Forbidden', 403);
}
}Successful presence auth response includes channel_data with user information:
{
"auth": "216e96a3506044bfd2b8b6eb7c492d32:e418ba60f634e52d6071795cbd5f4cee4388c789c8d9f513d4501960ec02e958",
"channel_data": "{\"user_id\":2024,\"user_info\":{\"name\":\"开源技术小栈\",\"age\":24}}"
}Client subscription example:
var privateChannel = connection.subscribe('presence-user-' + uid);Each member of a presence channel receives a user object containing id and user_info, enabling the application to display online user details.
Using presence channels adds higher security and lets you query user data of all subscribers.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
