OddSockets Java SDK

Official Java SDK for OddSockets real-time messaging platform

Java 11+ Maven Central Thread Safe High Performance Auto Reconnect

Overview & Features

The OddSockets Java SDK provides a robust, enterprise-ready interface for real-time messaging in Java applications, with full support for Spring Boot, Android, and standalone applications.

Java 11+ Support

Compatible with Java 11+ and all modern JVM languages including Kotlin and Scala.

Thread Safe

Fully thread-safe implementation with concurrent collections and proper synchronization.

Spring Boot Ready

Auto-configuration and starter dependencies for seamless Spring Boot integration.

High Performance

Optimized for low latency with efficient WebSocket connections and smart routing.

Cost Effective

No per-message pricing, industry-standard 32KB message limits, transparent pricing.

Automatic Failover

Built-in redundancy and intelligent error handling for 99.9% uptime.

Installation

xml
<dependency>
    <groupId>com.oddsockets</groupId>
    <artifactId>oddsockets-java-sdk</artifactId>
    <version>1.0.0</version>
</dependency>
gradle
implementation 'com.oddsockets:oddsockets-java-sdk:1.0.0'
scala
libraryDependencies += "com.oddsockets" % "oddsockets-java-sdk" % "1.0.0"

Quick Start

Basic Usage

java
import com.oddsockets.OddSockets;
import com.oddsockets.Channel;
import com.oddsockets.config.OddSocketsConfig;

public class BasicExample {
    public static void main(String[] args) {
        // Create configuration
        OddSocketsConfig config = new OddSocketsConfig.Builder()
            .apiKey("ak_live_1234567890abcdef")
            .build();

        // Initialize client
        OddSockets client = new OddSockets(config);

        // Get channel
        Channel channel = client.channel("my-channel");

        // Subscribe to messages
        channel.subscribe(message -> {
            System.out.println("Received: " + message.getData());
        });

        // Publish a message
        channel.publish("Hello, World!");
    }
}

Spring Boot Integration

java
@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private OddSockets oddSocketsClient;

    @PostMapping("/send")
    public ResponseEntity<String> sendMessage(@RequestBody ChatMessage message) {
        Channel channel = oddSocketsClient.channel("chat-room");
        
        try {
            channel.publish(message);
            return ResponseEntity.ok("Message sent successfully");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("Failed to send message");
        }
    }
}

Async/CompletableFuture Usage

java
import java.util.concurrent.CompletableFuture;

public class AsyncExample {
    public void asyncPublish() {
        OddSockets client = new OddSockets(config);
        Channel channel = client.channel("async-channel");

        // Async publish
        CompletableFuture<PublishResult> future = channel.publishAsync("Hello Async!");
        
        future.thenAccept(result -> {
            System.out.println("Message published: " + result.getMessageId());
        }).exceptionally(throwable -> {
            System.err.println("Publish failed: " + throwable.getMessage());
            return null;
        });
    }
}

Configuration

Client Configuration

java
OddSocketsConfig config = new OddSocketsConfig.Builder()
    .apiKey("ak_live_1234567890abcdef")     // Required: Your OddSockets API key
    .userId("user-123")                     // Optional: User identifier
    .autoConnect(true)                      // Optional: Auto-connect on creation
    .reconnectAttempts(5)                   // Optional: Max reconnection attempts
    .heartbeatInterval(30000)               // Optional: Heartbeat interval (ms)
    .timeout(10000)                         // Optional: Connection timeout (ms)
    .build();

Channel Options

java
// Subscribe with options
SubscribeOptions subscribeOptions = new SubscribeOptions.Builder()
    .enablePresence(true)                   // Enable presence tracking
    .retainHistory(true)                    // Retain message history
    .filter("user.premium == true")         // Message filter expression
    .build();

channel.subscribe(callback, subscribeOptions);

// Publish with options
PublishOptions publishOptions = new PublishOptions.Builder()
    .ttl(3600)                              // Time to live (seconds)
    .metadata(Map.of("priority", "high"))   // Additional metadata
    .storeInHistory(true)                   // Store in message history
    .build();

channel.publish(message, publishOptions);

Examples

Explore comprehensive examples demonstrating the OddSockets Java SDK in production applications:

Performance & Compatibility

OddSockets Java SDK delivers superior performance with broad JVM compatibility:

<50ms
Latency
99.9%
Uptime
32KB
Max Message
1M+
Messages/sec

JVM Support

  • Java 11+ (LTS)
  • Kotlin 1.8+
  • Scala 2.13+
  • Android API 26+

Framework Support

  • Spring Boot 2.7+
  • Micronaut 3.0+
  • Quarkus 2.0+
  • Jakarta EE 9+

Framework Integrations

The OddSockets Java SDK works seamlessly with all modern Java frameworks. Here are examples showing how to integrate with popular frameworks:

Spring Boot

java
@Configuration
@EnableConfigurationProperties(OddSocketsProperties.class)
public class OddSocketsConfig {

    @Bean
    @ConditionalOnMissingBean
    public OddSockets oddSocketsClient(OddSocketsProperties properties) {
        OddSocketsConfig config = new OddSocketsConfig.Builder()
            .apiKey(properties.getApiKey())
            .userId(properties.getUserId())
            .autoConnect(properties.isAutoConnect())
            .build();
            
        return new OddSockets(config);
    }
}

@Service
public class ChatService {
    
    @Autowired
    private OddSockets oddSocketsClient;
    
    @EventListener
    public void handleUserMessage(UserMessageEvent event) {
        Channel channel = oddSocketsClient.channel("chat-" + event.getRoomId());
        channel.publish(event.getMessage());
    }
}

Android

java
public class ChatActivity extends AppCompatActivity {
    private OddSockets oddSocketsClient;
    private Channel chatChannel;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        
        // Initialize OddSockets client
        OddSocketsConfig config = new OddSocketsConfig.Builder()
            .apiKey("ak_live_1234567890abcdef")
            .userId(getCurrentUserId())
            .build();
            
        oddSocketsClient = new OddSockets(config);
        chatChannel = oddSocketsClient.channel("mobile-chat");
        
        // Subscribe to messages
        chatChannel.subscribe(message -> {
            runOnUiThread(() -> {
                displayMessage(message);
            });
        });
    }
    
    private void sendMessage(String text) {
        ChatMessage message = new ChatMessage(getCurrentUserId(), text);
        chatChannel.publishAsync(message)
            .thenAccept(result -> {
                Log.d("Chat", "Message sent: " + result.getMessageId());
            })
            .exceptionally(throwable -> {
                Log.e("Chat", "Failed to send message", throwable);
                return null;
            });
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (oddSocketsClient != null) {
            oddSocketsClient.disconnect();
        }
    }
}

Kotlin

kotlin
import com.oddsockets.OddSockets
import com.oddsockets.config.OddSocketsConfig
import kotlinx.coroutines.*

class ChatService {
    private val oddSocketsClient: OddSockets
    private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
    
    init {
        val config = OddSocketsConfig.Builder()
            .apiKey("ak_live_1234567890abcdef")
            .userId("kotlin-user")
            .build()
            
        oddSocketsClient = OddSockets(config)
    }
    
    suspend fun subscribeToChannel(channelName: String) {
        val channel = oddSocketsClient.channel(channelName)
        
        channel.subscribe { message ->
            scope.launch {
                handleMessage(message)
            }
        }
    }
    
    suspend fun publishMessage(channelName: String, message: Any) {
        val channel = oddSocketsClient.channel(channelName)
        
        try {
            val result = channel.publishAsync(message).await()
            println("Message published: ${result.messageId}")
        } catch (e: Exception) {
            println("Failed to publish message: ${e.message}")
        }
    }
    
    private suspend fun handleMessage(message: Message) {
        // Process message on background thread
        withContext(Dispatchers.Default) {
            println("Received message: ${message.data}")
        }
    }
    
    fun cleanup() {
        scope.cancel()
        oddSocketsClient.disconnect()
    }
}

Micronaut

java
@Factory
public class OddSocketsFactory {
    
    @Singleton
    public OddSockets oddSocketsClient(@Property(name = "oddsockets.api-key") String apiKey,
                                       @Property(name = "oddsockets.user-id") Optional<String> userId) {
        OddSocketsConfig.Builder builder = new OddSocketsConfig.Builder()
            .apiKey(apiKey);
            
        userId.ifPresent(builder::userId);
        
        return new OddSockets(builder.build());
    }
}

@Controller("/api/chat")
public class ChatController {
    
    private final OddSockets oddSocketsClient;
    
    public ChatController(OddSockets oddSocketsClient) {
        this.oddSocketsClient = oddSocketsClient;
    }
    
    @Post("/send")
    public Single<HttpResponse<String>> sendMessage(@Body ChatMessage message) {
        Channel channel = oddSocketsClient.channel("micronaut-chat");
        
        return Single.fromFuture(channel.publishAsync(message))
            .map(result -> HttpResponse.ok("Message sent: " + result.getMessageId()))
            .onErrorReturn(throwable -> 
                HttpResponse.serverError("Failed to send message: " + throwable.getMessage()));
    }
}

Quarkus

java
@ApplicationScoped
public class OddSocketsProducer {
    
    @ConfigProperty(name = "oddsockets.api-key")
    String apiKey;
    
    @ConfigProperty(name = "oddsockets.user-id", defaultValue = "quarkus-user")
    String userId;
    
    @Produces
    @Singleton
    public OddSockets oddSocketsClient() {
        OddSocketsConfig config = new OddSocketsConfig.Builder()
            .apiKey(apiKey)
            .userId(userId)
            .build();
            
        return new OddSockets(config);
    }
}

@Path("/api/chat")
@ApplicationScoped
public class ChatResource {
    
    @Inject
    OddSockets oddSocketsClient;
    
    @POST
    @Path("/send")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Uni<Response> sendMessage(ChatMessage message) {
        Channel channel = oddSocketsClient.channel("quarkus-chat");
        
        return Uni.createFrom().completionStage(channel.publishAsync(message))
            .map(result -> Response.ok()
                .entity(Map.of("messageId", result.getMessageId()))
                .build())
            .onFailure().recoverWithItem(throwable ->
                Response.serverError()
                    .entity(Map.of("error", throwable.getMessage()))
                    .build());
    }
}