Skip to content

Commit

Permalink
Update WebSocket message parsing and types w/ feedback updates (#122)
Browse files Browse the repository at this point in the history
* Update WebSocket message parsing and types
  • Loading branch information
justinpolygon authored Aug 9, 2023
1 parent c92592d commit 23166d0
Show file tree
Hide file tree
Showing 11 changed files with 587 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public void onError(Throwable error) {
public static void websocketSample(String polygonKey) {
PolygonWebSocketClient client = new PolygonWebSocketClient(
polygonKey,
PolygonWebSocketCluster.Crypto,
Feed.RealTime.INSTANCE,
Market.Crypto.INSTANCE,
new DefaultPolygonWebSocketListener() {
@Override
public void onReceive(@NotNull PolygonWebSocketClient client, @NotNull PolygonWebSocketMessage message) {
Expand Down Expand Up @@ -111,15 +112,15 @@ public void onError(@NotNull PolygonWebSocketClient client, @NotNull Throwable e
public static void websocketLaunchpadSample(String polygonKey) {
PolygonWebSocketClient client = new PolygonWebSocketClient(
polygonKey,
PolygonWebSocketCluster.Crypto, // Available in Stocks, Options, Forex, Crypto
Feed.RealTime.INSTANCE,
Market.LaunchpadStocks.INSTANCE,
new DefaultPolygonWebSocketListener() {
@Override
public void onReceive(@NotNull PolygonWebSocketClient client, @NotNull PolygonWebSocketMessage message) {
if (message instanceof PolygonWebSocketMessage.LaunchpadMessage) {
System.out.println("Launchpad " + message);
} else {

System.out.println(message);
System.out.println(message.toString());
}
}

Expand All @@ -128,29 +129,21 @@ public void onError(@NotNull PolygonWebSocketClient client, @NotNull Throwable e
System.out.println("Error in websocket");
error.printStackTrace();
}
},
Channel.UNLIMITED,
new DefaultJvmHttpClientProvider(),
"launchpad.polygon.io");
});

client.connectBlocking();

List<PolygonWebSocketSubscription> subs = Collections.singletonList(
new PolygonWebSocketSubscription(PolygonWebSocketChannel.Launchpad.Aggregates.INSTANCE, "X:BTC-USD"));
new PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadStocks.AggPerMinute.INSTANCE, "TSLA"));
client.subscribeBlocking(subs);

List<PolygonWebSocketSubscription> subsVal = Collections.singletonList(
new PolygonWebSocketSubscription(PolygonWebSocketChannel.Launchpad.LaunchpadValue.INSTANCE, "X:BTC-USD"));
client.subscribeBlocking(subsVal);

try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}

client.unsubscribeBlocking(subs);
client.unsubscribeBlocking(subsVal);
client.disconnectBlocking();
}

Expand Down Expand Up @@ -197,4 +190,4 @@ public static void financialsSample(PolygonRestClient polygonRestClient) {
System.out.println(polygonRestClient.getExperimentalClient().getFinancialsBlocking(params));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun cryptoWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.RealTime,
Market.Crypto,
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.Trades, "ETH-USD"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.Trades, "BTC-USD")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.Trades, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.Quotes, "*"),
PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.AggPerSecond, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.AggPerMinute, "*")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Crypto.Level2Books, "*")
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000)
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun forexWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.RealTime,
Market.Forex,
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Forex.Quotes, "*"),
PolygonWebSocketSubscription(PolygonWebSocketChannel.Forex.AggPerSecond, "*"),
PolygonWebSocketSubscription(PolygonWebSocketChannel.Forex.AggPerMinute, "*"),
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000) // make sure we get a agg minute
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun indicesWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.RealTime,
Market.Indices,
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Indices.Value, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Indices.Value, "I:NDX"),
PolygonWebSocketSubscription(PolygonWebSocketChannel.Indices.AggPerSecond, "I:SPX")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Indices.AggPerMinute, "I:SPX")
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000)
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun launchpadWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.LaunchpadFeed,
Market.LaunchpadStocks, // replace with LaunchpadCrypto, LaunchpadForex, LaunchpadForex, etc
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadStocks.LaunchpadValue, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadStocks.AggPerMinute, "*")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.c.LaunchpadValue, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadOptions.AggPerMinute, "*")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadForex.LaunchpadValue, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadForex.AggPerMinute, "*")
//PolygonWebSocketSubscription(PolygonWebSocketChannel.LaunchpadCrypto.LaunchpadValue, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.c.AggPerMinute, "*")
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000)
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun optionsWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.RealTime,
Market.Options,
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
PolygonWebSocketSubscription(PolygonWebSocketChannel.Options.Trades, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Options.Quotes, "O:GOOGL230915P00067500"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Options.AggPerSecond, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Options.AggPerMinute, "*")
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000)
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.polygon.kotlin.sdk.sample

import io.polygon.kotlin.sdk.websocket.*
import kotlinx.coroutines.delay

suspend fun stocksWebsocketSample(polygonKey: String) {
val websocketClient = PolygonWebSocketClient(
polygonKey,
Feed.RealTime,
Market.Stocks,
object : PolygonWebSocketListener {
override fun onAuthenticated(client: PolygonWebSocketClient) {
println("Connected!")
}

override fun onReceive(
client: PolygonWebSocketClient,
message: PolygonWebSocketMessage
) {
when (message) {
is PolygonWebSocketMessage.RawMessage -> println(String(message.data))
else -> println("Receieved Message: $message")
}
}

override fun onDisconnect(client: PolygonWebSocketClient) {
println("Disconnected!")
}

override fun onError(client: PolygonWebSocketClient, error: Throwable) {
println("Error: ")
error.printStackTrace()
}

})

val subscriptions = listOf(
PolygonWebSocketSubscription(PolygonWebSocketChannel.Stocks.Trades, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Stocks.Quotes, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Stocks.AggPerSecond, "*"),
//PolygonWebSocketSubscription(PolygonWebSocketChannel.Stocks.AggPerMinute, "*")
)

websocketClient.connect()
websocketClient.subscribe(subscriptions)
delay(65_000)
websocketClient.unsubscribe(subscriptions)
websocketClient.disconnect()
}
Loading

0 comments on commit 23166d0

Please sign in to comment.