Simple client allowing to subscribe, authenticate, listen & consume Coinbase/Gdax websocket api. This client was written according to the following reference documentation: https://docs.pro.coinbase.com/#websocket-feed
- designed for spring-boot application in tomcat container
- with autoconfiguration enabled (@EnableAutoConfiguration), GdaxWebsocketClient bean is available without additional config
- supports all (documented) messages available (as of 08-2018) in the Coinbase/Gdax websocket feed
git clone https://github.com/miss-d/gdax-java-websocket-client.git
cd ./gdax-java-websocket-client
- ./gradlew build
cp ./build/libs/gdax-java-websocket-client-x.x.jar /{my-project}/lib
- add to project dependencies:
dependencies {
compile files('lib/gdax-java-websocket-client-x.x.jar')
}
- Implement
GdaxMessageHandler<T>
interface for handling message:
public class HeartbeatMessageHandler implements GdaxMessageHandler<Heartbeat> {
@Override
public void handleMessage(Heartbeat message) {
System.out.println("Hearbeat message arrived!"); //handle message here
}
}
- Implement
GdaxWebsocketClientConfigurer
& register message handlers
@Configuration
public class GdaxClientConfig implements GdaxWebsocketClientConfigurer {
@Override
public void addMessageHandlers(MessageHandlerRegistry messageHandlerRegistry) {
messageHandlerRegistry.addMessageHandler(MessageType.heartbeat, new HeartbeatMessageHandler());
//...
}
}
- Add configuration to your
application.yml
gdax-websocket-client:
enabled: true # true -> this client beans are available in the context
autoStartup: true # true -> connection is initialized right after context starts
wsUrl: "wss://ws-feed-public.sandbox.pro.coinbase.com" # api url - see https://docs.pro.coinbase.com/#websocket-feed
maxIdleTimeout: 1000 # max idle miliseconds before connection closes due to timeout
maxTextMessageBufferSize: 48490 # max message size (may be too small for snapshot messages)
auth: # generated auth data
key: "api key"
secret: "secret key"
passphrase: "secret passphrase"
@Autowire GdaxWebsocketClient & subscribe to one or many channels channels documentation:
Supported channels can be found in ChannelName.java
gdaxWebsocketClient.subscribe(new Subscription()
.forProductIds("ETH-USD")
.forChannels(ChannelName.heartbeat, ChannelName.full)
);
There is a default GdaxErrorMessageHandler registered for handling error messages from api. It just prints error level log if such message arrives. Can be overriden by implementing GdaxMessageHandler<ErrorMessage>
and registering as shown in Configuration.