xtpu
xtpu

Reputation: 55

websockets and javascript - transmitting name / value pairs

So there is a limit of 1 websocket connection per webpage in most browsers. Therefore, if I want my application to update several different items on a single page at the same time, I need to pass name/value pairs, e.g.:

number_of_notifications=5
latest_news_headline='Websockets are awesome!'
foo='bar'

My question is: what is the best cross-browser way to do this? The problem with simply sending variables as I showed above and then splitting them based on the '=' sign is that an equal sign in either the name or the value can break the script:

latest_news_headline='Websockets = awesome!'

My first thought was to transmit the name and value in base64 (in which case, I can use the = sign to delimit the data). Unfortunately, that's not supported in all browsers (I know that websockets aren't either, I'm actually using SockJS as a websocket emulator for the other browsers; the SockJS protocol, however, is identical to the websocket protocol, by design).

Upvotes: 0

Views: 76

Answers (1)

adrichman
adrichman

Reputation: 1235

It seems like stringified JSON is the obvious choice?

Upvotes: 2

Related Questions