In the subscribing project on the IAM page, add a new member with the Pub/Sub Publisher role, the new member name is the serviceaccount-email of from the publishing project.
Then create a cloud function in the publishing project and assign the same service-account in the bottom of the page(under more) to the function.
Here's a node example for a cloud function:
const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('projects/subscribing-project-name/topics/topic-to-
publish-to');
const publisher = topic.publisher();
exports.helloWorld = (req, res) => {
const customAttributes = {
message: 'Hello'
};
publisher.publish(Buffer.from("Hello from another project"), customAttributes,
(err) => {
if (err) {
res.status(500).send(JSON.stringify({ success: false, error: err.message }));
return;
}
res.status(200).send(JSON.stringify({ success: true, message: "Message sent to
pubsub topic" }));
});
};
GOOGLE_APPLICATION_CREDENTIALS
simply has to point to the certificate of a service account with access to the PubSub in the correct project. – ZackK