It sounds like you need to set up the bodyParser
which will parse the request body and make request.body
available.
See the Pusher docs: http://pusher.com/docs/authenticating_users#implementing_private_endpoints/lang=node
質問
I'm trying to create a private channel to allow users to send data to my node.js server. The request is failing, and returning a pusher:subscription_error with an error code of 500.
My node.js (server side) log cannot pull the socket_id from the incoming request to '/pusher/auth'
app.post( '/pusher/auth', function( req, res ) {
var socketId = req.body.socket_id;//<--DEBUG: TypeError: Cannot read property 'socket_id' of undefined
var channel = req.body.channel_name;
var presenceData = {
user_id: 'unique_user_id',
user_info: {
name: 'Mr Pusher',
twitter_id: '@pusher'
}
};
var auth = pusher.auth( socketId, channel, presenceData );
res.send( auth );
} );
Here is the client side which sends the request:
// Create new Pusher instance and connect
var key = "<%= appKey %>"
var id = "<%= appKey %>"
var secret = "<%= appSecret %>"
var pusher = new Pusher( "<%= appKey %>" );
// Subscribe to the channel that the event will be published on
var channel = pusher.subscribe( 'private-channel' );
channel.bind('pusher:subscription_succeeded',function(){
alert("subscription succeeded")
var triggered = channel.trigger("client-event",{})
alert("triggered "+triggered)
})
channel.bind('pusher:subscription_error',function(status){
alert("subscription error "+status)//<-- This error gets returned
})
// Bind to the event on the channel and handle the event when triggered
channel.bind( 'client-event', function( data ) {
// For now, alert the message.
alert( data.message );
} );
Should this be automatically handled by the pusher API, or do I need to explicitly send it? How can I send the socket_id to the server? How can I even access the socket_id variable on the client?
解決
It sounds like you need to set up the bodyParser
which will parse the request body and make request.body
available.
See the Pusher docs: http://pusher.com/docs/authenticating_users#implementing_private_endpoints/lang=node