Can the cometd library be used in Salesforce to listen to external buses?

I'm assuming you want this to be something running in Apex and not something that relies on Visualforce or Lightning Component.

If you want to have Salesforce subscribe to an event, you will need to publish to a Salesforce Platform Event. You can then write an Apex trigger in that event. There isn't a way to have cometd trigger Apex directly. See

You can subscribe to generic events in a Visualforce page or a Lightning Component, but that would require someone to be signed in and have the page open.

As of now, we do not have a way to subscribe to the external event bus which is always on. You have to use a middleware to subscribe to external event bus and then push same into SF event bus.

We use Mulesoft Anypoint platform as a middleware.

Anypoint platform reads from External event bus using Amazon SQS connector , transforms data to SF Data model form and publishes into Salesforce Event Bus.

If the external bus supports the Bayeux protocol, then you could use the same CometD library and code to subscribe to both the Salesforce bus and the external bus. The solution in this case would be running off-platform (ie: Heroku, Azure, AWS) because there isn't an on-platform option to subscribe to an event bus outside of Salesforce. The subscriber would republish the event as a Salesforce platform event using the REST, SOAP, or Bulk API. While this is theoretically possible, most buses use different protocols (STOMP, MQTT, AMQP, WebSockets, direct Kafka, etc).

TLDR: technically possible, although not likely.