1

I am considering using Socket.IO over WebSocket (which I am currently using) in my Project. I have not used Node.js yet. When i tried to go through Socket.IO, I got the idea that Node.js is a must to implement this. I got a few questions to ask.

  1. Is it similar to WebSocket
  2. Can I use Socket.IO without Node.js
  3. Where can I learn to use Socket.IO with Servlets
  4. Do I need to add separate library files to work with Socket.IO in Servlets(I am using Java 8). If yes, Which is the right one.

I am using Glass-fish Server. Only hibernate is used in my project, no other frameworks are used and I am not familiar with any other.

I went through this question previously asked here , but i don't understand how to use this . Please Help

Community
  • 1
  • 1
sanal
  • 93
  • 10

1 Answers1

6

1) Yes. It is similar to pure websocket. But netty-socketio:

where

  • 4 - message's packet type;
  • 2 - event's inner type;
  • eventName - a name of your event, it must be the same in your Java @OnEvent handler (see below).

You can organize your_data_objects as you wish, for example, write something like HTTP:

42["eventName", {headers map}, data(primitive, array or object)]

42["createUser", {"requestId": "UUID"}, {
    "firstName": "John",
    "lastName": "Dow"
}]

42["deleteUser", {}, 10]

42["getUsers", {}, [1,2,3,4,5]]

and etc.

  • require connect via http to

    ws://host/socket.io/?transport=websocket

(transport setting is required);

  • require sending ping-pong packets
  • 2 - PING,
  • 3 - PONG

between your client and server or disabling it in a server's configuration (not recommended, it is useful for dead sessions).

  • supports easy to write authorization on handshake stage;

  • supports rooms. You can join/leave rooms (based on pub/sub in Hazelcast/Redisson) and send broadcast events only for these rooms;

  • supports acknowledge callbacks (can be disabled);

In conclusion, netty-socket-io is very useful, stable instrument and can be used in production.

2) Absolutely. You can use netty-socket-io on your Java backend side. And socket-io library on your client-side. Or even a pure websocket:

var ws = new Websocket("ws://localhost:9060/socket.io/?transport=websocket");

setInterval(function(){ sendHeartbeat(); }, 5000);

function sendHeartbeat() {
    ws.send("2");
}

ws.onopen = function() {
    ws.send('42["doSomething", {"header1": "value1"}, {"key1": "value1"}');
}

Java:

public class Controller {

    @OnEvent("doSomething")
    public void doSomething(SocketIOClient client, Headers headers, Something something) {
        client.sendEvent("doSomethingSuccess", null, responseHeaders, responseObject);
    }
}

And route to controller in configuration:

server.addListeners(new Controller()); 

3) Sadly, there is little information about netty-socket-io, you can just try it and create new issues when you in troubles;

4) You need to add netty-socket-io to your dependencies. Hazelcast or Redisson if you are going to use them for storing sessions and subscriptions to rooms. Jackson for json serializing/deserializing. That's all.

Ivan
  • 465
  • 5
  • 19