Menu

Network Programming Examples

Relevant source files

This page demonstrates practical network programming in OwnLang through concrete examples that showcase HTTP client communication and real-time socket programming. The examples illustrate integration with external APIs, asynchronous request handling, and bidirectional socket communication patterns.

For information about the underlying network module implementations, see Network and HTTP Modules. For server-side web programming, see Server Module.

HTTP Client Programming

The GitHub timeline example demonstrates HTTP client capabilities using the http module to consume REST APIs. This example fetches public GitHub events and processes the JSON response data.

Basic HTTP Request Pattern

The GitHub timeline fetcher shows both synchronous and asynchronous HTTP request patterns:

The example demonstrates the thread() function for non-blocking HTTP requests examples/network/github_timeline.own14-16 The callback function receives the response data and processes it using jsondecode() to parse JSON.

Sources: examples/network/github_timeline.own

API Response Processing

The GitHub example showcases comprehensive JSON data processing with pattern matching for different event types:

Event TypeHandler PatternResponse Data
CommitCommentEventExtract comment bodypayload.comment.body
CreateEventRepository creationpayload.ref_type
PushEventCommit countlength(payload.commits)
IssuesEventIssue actionspayload.action, payload.issue.title

The github_event_type() function examples/network/github_timeline.own25-40 uses pattern matching to handle different GitHub webhook event structures, demonstrating robust API response processing.

Sources: examples/network/github_timeline.own

Real-time Socket Programming

The pipes online game demonstrates Socket.io client integration for real-time multiplayer communication. The socket connection enables bidirectional event-based messaging between clients.

Socket Connection and Event Handling

The socket client initialization examples/game/pipes_online.own89-110 establishes connection and registers event handlers for game state synchronization.

Sources: examples/game/pipes_online.own

Bidirectional Game State Synchronization

The networking layer synchronizes game state between multiple clients through structured event messaging:

DirectionEvent NameData StructurePurpose
Server → ClientgameStartdata[0] (board array)Initialize game board
Server → ClientupdateGhostCell{x: int, y: int}Sync opponent moves
Server → ClientupdateGhostCursor{x: int, y: int}Track opponent cursor
Client → ServerupdateCursor{x: curX, y: curY}Broadcast cursor position
Client → ServerswitchCell{x: curX, y: curY}Send game action

The game loop processes input and emits corresponding socket events examples/game/pipes_online.own117-132 demonstrating real-time input synchronization patterns.

Sources: examples/game/pipes_online.own

Network Programming Patterns

Asynchronous Request Handling

OwnLang provides multiple approaches to handle network operations without blocking the main thread:

The thread() function enables asynchronous HTTP requests while maintaining callback-based response handling examples/network/github_timeline.own14

Sources: examples/network/github_timeline.own

Error Handling and Connection Management

Network programming requires proper connection lifecycle management:

  • Connection establishment: newSocket(url) followed by socket.connect()
  • Event registration: Chain .on(event, handler) calls before connection
  • Graceful shutdown: socket.disconnect() in cleanup code
  • Timeout handling: Server-side timeout configuration for HTTP requests

The pipes game properly disconnects the socket when exiting the game loop examples/game/pipes_online.own173

Sources: examples/game/pipes_online.own

Module Integration

Network examples demonstrate integration between multiple OwnLang modules:

ModuleUsage PatternExample
httpAPI consumptionhttp(url, callback)
jsonResponse parsingjsondecode(response)
socketReal-time communicationnewSocket(url).on(event, handler)
stdThreading and utilitiesthread(function, args...)
functionalData processingforeach(array, processor)

These examples showcase how OwnLang's modular architecture enables comprehensive network programming capabilities through module composition.

Sources: examples/network/github_timeline.own, examples/game/pipes_online.own