Senior Lead - Solutions Engineer, WSO2
File Processing and Websockets with
Ballerina
Chanaka Fernando
File Processing with Ballerina
• File-based data transfers are here to stay
• Cloud and on premise
• Extract, Transform, Load (ETL) requirements
• Different file formats
• Streaming large files
• Securely transfer files
File based data sharing
• File I/O as part of the Ballerina I/O
• Fine grained file access (Bytes, Characters)
• Coarse grained file access (Records)
• Large file transfer through streaming
• Connect with remote file systems (FTP,
SFTP, SMB, etc..)
File Processing with Ballerina
• Open
• List
• Create directory
• Copy
• Move
• Create new file
• Delete
Ballerina File API - Operations
function main (string[] args) {
file:File target = {path:"/tmp/result.txt"};
target.open(file:W);
boolean b = target.exists();
println("file exists: " + b);
file:File source = {path:"/tmp/result.txt"};
file:File destination = {path:"/tmp/copy.txt"};
file:copy(source, destination);
println("file copied: /tmp/result.txt to /tmp/copy.txt");
destination.delete();
println("file deleted: /tmp/copy.txt");
destination = {path:"/tmp/move.txt"};
file:move(source, destination);
}
Ballerina File API - Example
Open
Exists
Copy
Delete
Move
Ballerina I/O API
I/O APIs
Record I/O
RecordsCharacters
Bytes
I/O Channels: Files, Sockets, Devices, ….
BytesI/O
CharactersI/O
Console
I/O
DataI/O
Different record formats
• Read byte by byte
• Character by character
• Record by record
• Canonical byte representation
• Console I/O, Data I/O (Future)
Ballerina I/O API - Files
Read Characters From File
Read a Record From File
• Support connecting with remote file systems
over FTP/SMB/SFTP (in-development)
• Secure file transfer through encryption
Future Improvements
Websockets with Ballerina
• Modern applications (especially web)
consumes large amount of data in real time
• Request-Response pattern no longer
sufficient
• HTTP (1.1 and prior) lives with hacks (AJAX
long polling, Streaming, etc.)
Why Websockets?
• Long-lived connections
• Bi-directional messaging (full-duplex)
• Low usage of headers (save bandwidth)
• Co-exists with HTTP
With Websockets
• Support for different frame types (text, binary,
ping, pong)
• Sub protocol support
• SSL support
• Live alongside HTTP
Websockets in Ballerina
• Expose ballerina service through websockets
• Executes different logic based on message
type (text, binary, ping, pong)
• Can configure base path and port which is
exposing
Websockets Server connector
Websockets Server Connector
Websockets service definition
Client initiates a connection
Client connected
Text frame received
Connection closed
Service configuration
Websockets Client Connector
• Creates a connection with a websocket service
• Supports sub protocols
• Text, Binary, Ping, Pong message types
• Supports security
• Visit Ballerina website
https://ballerinalang.org/
• Start with examples
https://ballerinalang.org/docs/by-example/
• Contribute
https://github.com/ballerinalang
Download and getting started
19
wso2.com

[WSO2Con EU 2017] File Processing and Websockets with Ballerina

  • 1.
    Senior Lead -Solutions Engineer, WSO2 File Processing and Websockets with Ballerina Chanaka Fernando
  • 2.
  • 3.
    • File-based datatransfers are here to stay • Cloud and on premise • Extract, Transform, Load (ETL) requirements • Different file formats • Streaming large files • Securely transfer files File based data sharing
  • 4.
    • File I/Oas part of the Ballerina I/O • Fine grained file access (Bytes, Characters) • Coarse grained file access (Records) • Large file transfer through streaming • Connect with remote file systems (FTP, SFTP, SMB, etc..) File Processing with Ballerina
  • 5.
    • Open • List •Create directory • Copy • Move • Create new file • Delete Ballerina File API - Operations
  • 6.
    function main (string[]args) { file:File target = {path:"/tmp/result.txt"}; target.open(file:W); boolean b = target.exists(); println("file exists: " + b); file:File source = {path:"/tmp/result.txt"}; file:File destination = {path:"/tmp/copy.txt"}; file:copy(source, destination); println("file copied: /tmp/result.txt to /tmp/copy.txt"); destination.delete(); println("file deleted: /tmp/copy.txt"); destination = {path:"/tmp/move.txt"}; file:move(source, destination); } Ballerina File API - Example Open Exists Copy Delete Move
  • 7.
    Ballerina I/O API I/OAPIs Record I/O RecordsCharacters Bytes I/O Channels: Files, Sockets, Devices, …. BytesI/O CharactersI/O Console I/O DataI/O Different record formats
  • 8.
    • Read byteby byte • Character by character • Record by record • Canonical byte representation • Console I/O, Data I/O (Future) Ballerina I/O API - Files
  • 9.
  • 10.
    Read a RecordFrom File
  • 11.
    • Support connectingwith remote file systems over FTP/SMB/SFTP (in-development) • Secure file transfer through encryption Future Improvements
  • 12.
  • 13.
    • Modern applications(especially web) consumes large amount of data in real time • Request-Response pattern no longer sufficient • HTTP (1.1 and prior) lives with hacks (AJAX long polling, Streaming, etc.) Why Websockets?
  • 14.
    • Long-lived connections •Bi-directional messaging (full-duplex) • Low usage of headers (save bandwidth) • Co-exists with HTTP With Websockets
  • 15.
    • Support fordifferent frame types (text, binary, ping, pong) • Sub protocol support • SSL support • Live alongside HTTP Websockets in Ballerina
  • 16.
    • Expose ballerinaservice through websockets • Executes different logic based on message type (text, binary, ping, pong) • Can configure base path and port which is exposing Websockets Server connector
  • 17.
    Websockets Server Connector Websocketsservice definition Client initiates a connection Client connected Text frame received Connection closed Service configuration
  • 18.
    Websockets Client Connector •Creates a connection with a websocket service • Supports sub protocols • Text, Binary, Ping, Pong message types • Supports security
  • 19.
    • Visit Ballerinawebsite https://ballerinalang.org/ • Start with examples https://ballerinalang.org/docs/by-example/ • Contribute https://github.com/ballerinalang Download and getting started 19
  • 20.