Reputation: 9226
Im building a Chat website that uses Websockets(Socket.io)
to send and receive messages to the server. In fact my website should use Websocket
and now my problem is that for other transmissions like checking username at login or fetching JSON
and updation DOM
and other stuff, Can i use the same technology(Websockets
) or i have to use Ajax? i know that in Websockets
way, server and client will have a persistant connection.
What is the best way? Using Websockets
is not good for these purposes? Why?
Upvotes: 2
Views: 129
Reputation: 1986
You can use websockets. The difference is with websockets the client is always connected. You'll have a handler that handles messages (which could be just json blobs maybe with some kind of messageType field) as they stream in from the client.
This means the server side handling is basically the same except instead of serving your responses via over different HTTP request (via different routes), you dispatch the request to the appropriate handler by something not much more complicated than a switch statement. Any results are then sent back to client over the websocket which has a similar handling mechanism.
One downside is not all browsers support websockets so if you need to support a fallback path to JSON then it's certainly easier to use the fallback JSON handlers for the aux requests (since you'll be writing them anyway).
Otherwise the differences are probably marginal. I'd be more concerned about code cleanliness.
Upvotes: 2