-1

I have a task to create javascript realtime app. Server side is ready (wss://), I have to create client-side.

The main tasks I have problems with:

The client can ping the server to check your connectivity. Client does a ping, including the sequence number (which allows to trace the exact ping duration).

{
  "$type": "ping",
  "seq": 1
}

server will respond:

{
  "$type": "pong",
  "seq": 1
}

Client request

{
  "$type": "subscribe_tables"
}

Server will respond with the list of tables, and update the client with table_added, table_removed and table_updated messages whenever the status has changed.

{
  "$type": "table_list",
  "tables": [
    {
      "id": 1,
      "name": "table 1",
      "description" : "one, two"
    }, {
      "id": 2,
      "name": "table 2"
      "description" : "two, three"
    }
  ]
}

table_updated event

{
  "$type": "update_table",
  "table": {
    "id": 3,
    "name": "table - Foo Fighters",
    "participants": 4
  }
}

Question: I know, that I can use new EventSource(), is this correct? How can I send data $type, for example with it?

MurDaD
  • 362
  • 3
  • 10
  • It's odd you would say the "server is ready" when you don't even yet know how your client is going to connect and get the data. How are you planning on getting data from the client: http requests, webSocket or server side events? – jfriend00 Jul 18 '16 at 17:26
  • I can connect to the server via wss:// – MurDaD Jul 18 '16 at 17:28
  • So, if a webSocket is your plan, then why are you talking about using `new EventSource()`. Right now, your whole question doesn't really make sense. We can't tell what you're actually asking for help with. If you're connecting on a webSocket, then you just send data from the server over the webSocket and read that data off the webSocket on the client. – jfriend00 Jul 18 '16 at 17:29
  • I've asked if it's correct to use EventSource(), if not - what I have to use? – MurDaD Jul 18 '16 at 17:30
  • You would only use `EventSource` if you are using server-side events which is different than webSocket and has much less browser support than webSockets. No, it's not correct to use EventSource if your communication mechanism is webSockets. You send and receive data on the webSocket. – jfriend00 Jul 18 '16 at 17:31
  • FYI, to send a compound data structure like you show, you would typically use `JSON.stringify()` on the data to serialize it into a string, send that and then use `JSON.parse()` on the other end to de-serialize it back into a Javascdript object. If you use a layer like socket.io that runs on top of a webSocket, then it will do that for you automatically. – jfriend00 Jul 18 '16 at 17:35
  • @jfriend00 the question was: what I have to use. And the answer is `new WebSocket()`, instead of `new EventSource()` – MurDaD Jul 18 '16 at 17:39
  • Yeah, but your question doesn't anywhere mention that you've written a server to handle an incoming webSocket connection and that's what you plan to use to send the data. It's kind of hard to know that's what your question means when you don't tell us that core piece of information. So, if that is actually the question, then of course, you create a webSocket connection in the client and use its API for sending or receiving data. Read your own question again and look at what you have marked as "Question" and ask yourself how we would have any idea you were using a websocket? – jfriend00 Jul 18 '16 at 17:41

1 Answers1

0

I know, that I can use new EventSource(), is this correct? How can I send data $type, for example with it?

No, that is not correct. If your server is expecting a webSocket connection, then you use new WebSocket(...) from the client to make the connection from client to server. And EventSource() object is used for server-side events which is a completely different transport from webSocket.

You can see programming examples here on MDN for using webSocket.

Also, if you are trying to send Javascript objects as data, you would typically use JSON.stringify() to serialize them into a string and then send that and then on the receiving end, you would use JSON.parse() to parse the JSON string back into a Javascript object. That's how you would send info like your $type along with other data in the same message.

FYI, socket.io is a library built on top of webSocket that has become very popular because it does a lot of things for your automatically that are typically needed in webSocket programming such as JSON serialization, automatic reconnect, automatic keep alive, connection drop detection, etc... You certainly don't have to use it in client and server, but it often saves a lot of time.

jfriend00
  • 683,504
  • 96
  • 985
  • 979