# cettia-java-server
**Repository Path**: github-codes/cettia-java-server
## Basic Information
- **Project Name**: cettia-java-server
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-11-25
- **Last Updated**: 2022-02-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Cettia
Cettia is a full-featured real-time web application framework for Java that you can use to exchange events between server and client in real-time. *It is meant for when you run into issues which are tricky to resolve with WebSocket, JSON, and switch statement per se:*
- Avoiding repetitive boilerplate code
- Supporting environments where WebSocket is not available
- Handling both text and binary data together
- Recovering missed events
- Providing multi-device user experience
- Scaling out an application, and so on.
It offers a reliable full duplex message channel and elegant patterns to achieve better user experience in the real-time web, and is compatible with any web frameworks on the Java Virtual Machine.
---
The following is a summary of the Cettia starter kit to help you get started quickly. In the summary, comments starting with `##` refer to a title of a related chapter in the tutorial, [Building Real-Time Web Applications With Cettia](https://cettia.io/guides/cettia-tutorial/), where you can find a detailed explanation. You may want to highlight the `##`.
Maven dependencies.
```xml
io.cettiacettia-server1.2.0io.cettia.asityasity-bridge-servlet32.0.0io.cettia.asityasity-bridge-jwa12.0.0
```
A class to play with the Cettia server. Import statements, verbose try-catch blocks, empty methods, etc. are skipped for brevity.
```Java
@WebListener
public class CettiaConfigListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) {
// Cettia part
// If you don't want to form a cluster,
// replace the following line with 'Server server = new DefaultServer();'
ClusteredServer server = new ClusteredServer();
HttpTransportServer httpAction = new HttpTransportServer().ontransport(server);
WebSocketTransportServer wsAction = new WebSocketTransportServer().ontransport(server);
// If a client opens a socket, the server creates and passes a socket to socket handlers
server.onsocket((ServerSocket socket) -> {
// ## Socket Lifecycle
Action logState = v -> System.out.println(socket + " " + socket.state());
socket.onopen(logState).onclose(logState).ondelete(logState);
// ## Sending and Receiving Events
// An 'echo' event handler where any received echo event is sent back
socket.on("echo", data -> socket.send("echo", data));
// ## Attributes and Tags
// Attributes and tags are contexts to store the socket state in the form of Map and Set
String username = findParam(socket.uri(), "username");
if (username == null) {
// Attaches a tag to the socket
socket.tag("nonmember");
} else {
// Associates an attribute with the the socket
socket.set("username", username);
}
// ## Working with Sockets
// A 'chat' event handler to send a given chat event to every socket in every server in the cluster
socket.on("chat", data -> server.find(ServerSocketPredicates.all()).send("chat", data));
// ## Advanced Sockets Handling
if (username != null) {
// A group of sockets representing the same user, whose username is the given one
Sentence user = server.find(ServerSocketPredicates.attr("username", username));
// A 'myself' event handler to send a given myself event to myself whose username is the same
socket.on("myself", data -> {
// You can directly handle each socket through the execute method of Sentence if needed
user.execute(s -> s.send("myself"));
});
// Limits only one socket per user
boolean onlyOneSocket = Boolean.parseBoolean(findParam(socket.uri(), "onlyOneSocket"));
if (onlyOneSocket) {
// Finds sockets opened by whose username is the same other than this socket and
// sends a 'signout' event to prevent reconnection and closes a connection
user.find(id(socket).negate()).send("signout").close();
}
}
// ## Disconnection Handling
Queue