Sanic with websocket and SocketIO. Which one to use? - python-3.x

Using Sanic, you can handle websockets connections using #app.websocket('/socket.io/') but the protocol used in Socket.io is specific.
Python has Python-Socketio as a module to handle Socket.Io specific communications, but they recommend to use the code like this :
sio = socketio.AsyncServer(async_mode='sanic')
app = Sanic()
sio.attach(app)
#sio.on('connect')
def on_connect():
...
So, which one should be used? Should we implement SocketIo protocol inside #app.websocket from Sanic, or should we ignore this and directly use the implementation from SocketIo?
I'm asking for both rapidity and best practice here. If the best decision is to go with #app.websocket, how can we set up Socket.io inside the Sanic handler?

The Socket.IO protocol is very complex, it will take you a decent amount of time to implement it all manually in the websocket route. Also note that Socket.IO uses the same route for HTTP and WebSocket, something that I understand it is not possible to do (easily, at least) with Sanic.
The python-socketio package integrates with many frameworks, including Sanic to implement all the details of the Socket.IO protocol.

Related

Should I rewrite HTTP server in socket.io

I have the current APIs running in an express server along with MongoDB in form of HTTP requests response. I currently have the use case of a messaging system that I know requires web sockets.
Should I rewrite the whole APIs in socket.io? Or Is there any option to do it on top of the existing HTTP server
Any suggestions in this situation will be helpful.
No, the are not mutually exclusive. Keep using HTTP for things that make sense as APIs, and only add sockets for the things that require bi-directional messaging.
socket.io is not required, and it's usually better to just use plain websockets. socket.io is a old, large framework that's not needed.

What is the best framework and difference of nodejs framework

These days I try to develop real time application using nodejs.
That application want to update the dashboard according to api data.
I installed express and faye and try to compare what is the best and what are the differences of that two.
As I know express is a node base framework and faye is a subscriber/publisher based one.
But I think both are almost same and anyone can help me to identify the differences?
What is fast and what can be done using the frameworks like that ?
Thanks in advance.
They are not very comparable. If you want to create a real-time application, you will probably need to use both.
Express is a web framework. You will need it to serve and handle HTTP requests and responses. It will help you handle things like url routing, request/response handling middleware, interfacing with template engines, etc... Express is as fast as you'll get.
Faye is a pub sub messaging system- It will not be capable of handling standard HTTP requests and responses. You may be able to implement a live stream of the data using Faye, however, you will still have the need to serve up your client side application using Express.
I would also look into Socket.io as an alternative to Faye- in addition to Express.

What's different between connect framework and socket.io?

I'm new to node.js, so i have some questions about connect framework and socket.io:
What's different? i'm confused about it.
Should i use connect fw with socket.io or just use socket.io?
The Connect module is a web application framework, while Socket.IO is a realtime transport module. You would use one to create web applications, and the other for bidirectional communication between a server and a client.
Here's a few of the things the modules can do:
Connect:
service static files and pages
provide cookie-based sessions
accept file uploads
handle HTTP verbs (GET/POST/PUT/DELETE)
Socket.IO:
authorize connecting sockets
send data between server and client with multiple transports
supports (WebSocket/XHR long-polling/flashsocket/JSONP)
So if you wanted to create a website, you would use Connect. However, if you wanted that website to have something such as realtime chat capability, then you would use Socket.IO.
Whether you should use one module or the other, or use them together, is dependent on your application requirements.
Connect is special module which can provide scalable functionality. You can just add features as middleware. It reminds some kind of configuration of your project, it just simplify routine.
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(connect.bodyParser())
.listen(3000);
After adding this for example you can access features which connect provide. For example you can have logging (method url and seconds) for each application activity, or add session support, easy with one line of code. The same way you can add socket support I suppose.

Connecting to socket.io from pure js

Hi I looking for a documentation or example to connect to socket.io running on nodejs at server side from a pure JS from a client.
I have to remove the socket.io.js lib from the client side.
socket.io provides various transport layers. If you are happy to experiment and are sure that your client supports WebSocket in all cases, then you might be successful in reproducing the WebSocket-based part of the socket.io protocol on the client side. This, however, seems to be a tedious task compared to just using the code that you would re-invent (partly) anyway.
Use socket.io on the client side, or take an entirely different approach, also on the server side.

Scalable Node.js 1x1 chat platform

I am to build a chat platform over Node.js, that in core, must be able to to provide chat screens 1-1, much like Olark provides
The chat update rate is not priority, but scalability and browser compatibility are.
My question is: Which back-end strategy and which way to transmit, would be best?
EDIT:
Thanks, #Brandon_R. It is just that I am not sure if websocket is the way to go here, I am between it and AJAX.
I want my server to be able to host multiple calls, and websocket do keep a open connection for each client; isn't it limiting?
Socket.io falls back on ajax polling/other transports if websockets are not available and is probably the way to go. You can also disable websockets/other transports if you prefer not to use them.
socket.io 0.8 also has support for "rooms" which will namespace and multiplex your sockets.

Resources