HTML and CSS Reference
In-Depth Information
};
var
socketId
=
getSocketId
();
if
(
socketId
!==
null
) {
data
.
socket_id
=
socketId
;
}
postComment
(
data
);
return
false
;
}
function
getSocketId
() {
if
(
pusher
&&
pusher
.
connection
.
state
===
'connected'
) {
return
pusher
.
connection
.
socket_id
;
}
return null
;
}
The changes we've made are:
•
A new function called
getSocketId
has been added to get the
socket_id
. It wraps a
check to ensure that the
pusher
variable has been set and also that the client is
connected to Pusher.
•
The
handleSubmit
has been updated to check to see if a
socket_id
is available. If it is,
this information is posted to the server along with the comment data.
On the server we need to use the
socket_id
parameter if it is present and therefore exclude
the connection and user who submitted the comment, or pass in
null
if it's not:
$channel_name
=
'comments-'
.
$added
[
'comment_post_ID'
];
$event_name
=
'new_comment'
;
$socket_id
=
(
isset
(
$_POST
[
'socket_id'
])
?
$_POST
[
'socket_id'
]:
null
);
$pusher
=
new
Pusher
(
APP_KEY
,
APP_SECRET
,
APP_ID
);
$pusher
-&
gt
;
trigger
(
$channel_name
,
$event_name
,
$added
,
$socket_id
);