correct way to call javascript into hook function

By adding the function to the publish_post hook it executes when the post gets published as the page is refreshing. That’s whats causing the headers already sent message.

This looks like some sort of chat that gets saved back to the database. If this needs to run on the front end of the site you could hook into wp_footer:

add_action('wp_footer', 'node_wp' );

In your node_wp function you can get the id using

global $post; $post_id = $post->ID;