Added an optional callback to publish function